Skip to content

Commit

Permalink
issue #180: fix inconsequential memory leak in write_caff.c found in …
Browse files Browse the repository at this point in the history
…static analysis
  • Loading branch information
dbry committed Mar 15, 2024
1 parent 847ab34 commit ec6914c
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions cli/caff_write.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ int WriteCaffHeader (FILE *outfile, WavpackContext *wpc, int64_t total_samples,
int float_norm_exp = WavpackGetFloatNormExp (wpc);
uint32_t channel_layout_tag = WavpackGetChannelLayout (wpc, NULL);
unsigned char *channel_identities = malloc (num_channels + 1);
unsigned char *new_channel_order = NULL;
int num_identified_chans, i;

if (float_norm_exp && float_norm_exp != 127) {
error_line ("invalid float data for CAFF, use --normalize-floats and omit MD5 check!");
free (channel_identities);
return FALSE;
goto error_exit;
}

// get the channel identities (including Microsoft) and count up the defined ones
Expand All @@ -117,7 +117,7 @@ int WriteCaffHeader (FILE *outfile, WavpackContext *wpc, int64_t total_samples,

if (!DoWriteFile (outfile, &caf_file_header, sizeof (caf_file_header), &bcount) ||
bcount != sizeof (caf_file_header))
return FALSE;
goto error_exit;

// format and write the Audio Description Chunk

Expand All @@ -127,7 +127,7 @@ int WriteCaffHeader (FILE *outfile, WavpackContext *wpc, int64_t total_samples,

if (!DoWriteFile (outfile, &caf_desc_chunk_header, sizeof (caf_desc_chunk_header), &bcount) ||
bcount != sizeof (caf_desc_chunk_header))
return FALSE;
goto error_exit;

caf_audio_format.mSampleRate = (double) sample_rate;
memcpy (caf_audio_format.mFormatID, "lpcm", sizeof (caf_audio_format.mFormatID));
Expand All @@ -144,7 +144,7 @@ int WriteCaffHeader (FILE *outfile, WavpackContext *wpc, int64_t total_samples,

if (!DoWriteFile (outfile, &caf_audio_format, sizeof (caf_audio_format), &bcount) ||
bcount != sizeof (caf_audio_format))
return FALSE;
goto error_exit;

// we write the Channel Layout Chunk if any of these are true:
// 1. a specific CAF layout was specified (100 - 147)
Expand All @@ -170,7 +170,7 @@ int WriteCaffHeader (FILE *outfile, WavpackContext *wpc, int64_t total_samples,

if (!DoWriteFile (outfile, &caf_chan_chunk_header, sizeof (caf_chan_chunk_header), &bcount) ||
bcount != sizeof (caf_chan_chunk_header))
return FALSE;
goto error_exit;

if (channel_layout_tag) {
if (debug_logging_mode)
Expand All @@ -192,11 +192,10 @@ int WriteCaffHeader (FILE *outfile, WavpackContext *wpc, int64_t total_samples,

if (!DoWriteFile (outfile, &caf_channel_layout, sizeof (caf_channel_layout), &bcount) ||
bcount != sizeof (caf_channel_layout))
return FALSE;
goto error_exit;
}
else { // write a channel description array because a single layout or bitmap won't do it...
CAFChannelDescription caf_channel_description;
unsigned char *new_channel_order = NULL;
int i;

if (debug_logging_mode)
Expand All @@ -220,7 +219,7 @@ int WriteCaffHeader (FILE *outfile, WavpackContext *wpc, int64_t total_samples,

if (!DoWriteFile (outfile, &caf_chan_chunk_header, sizeof (caf_chan_chunk_header), &bcount) ||
bcount != sizeof (caf_chan_chunk_header))
return FALSE;
goto error_exit;

caf_channel_layout.mChannelLayoutTag = kCAFChannelLayoutTag_UseChannelDescriptions;
caf_channel_layout.mChannelBitmap = 0;
Expand All @@ -229,7 +228,7 @@ int WriteCaffHeader (FILE *outfile, WavpackContext *wpc, int64_t total_samples,

if (!DoWriteFile (outfile, &caf_channel_layout, sizeof (caf_channel_layout), &bcount) ||
bcount != sizeof (caf_channel_layout))
return FALSE;
goto error_exit;

for (i = 0; i < num_channels; ++i) {
unsigned char chan_id = new_channel_order ? channel_identities [new_channel_order [i]] : channel_identities [i];
Expand All @@ -247,11 +246,8 @@ int WriteCaffHeader (FILE *outfile, WavpackContext *wpc, int64_t total_samples,

if (!DoWriteFile (outfile, &caf_channel_description, sizeof (caf_channel_description), &bcount) ||
bcount != sizeof (caf_channel_description))
return FALSE;
goto error_exit;
}

if (new_channel_order)
free (new_channel_order);
}
}

Expand All @@ -268,16 +264,21 @@ int WriteCaffHeader (FILE *outfile, WavpackContext *wpc, int64_t total_samples,

if (!DoWriteFile (outfile, &caf_data_chunk_header, sizeof (caf_data_chunk_header), &bcount) ||
bcount != sizeof (caf_data_chunk_header))
return FALSE;
goto error_exit;

mEditCount = 0;
WavpackNativeToBigEndian (&mEditCount, "L");

if (!DoWriteFile (outfile, &mEditCount, sizeof (mEditCount), &bcount) ||
bcount != sizeof (mEditCount))
return FALSE;
goto error_exit;

free (new_channel_order);
free (channel_identities);

return TRUE;

error_exit:
free (new_channel_order);
free (channel_identities);
return FALSE;
}

0 comments on commit ec6914c

Please sign in to comment.