Skip to content

Commit 16e52e5

Browse files
committed
Add alloc_check throughout libFLAC allocations
1 parent cfe3afc commit 16e52e5

File tree

6 files changed

+25
-24
lines changed

6 files changed

+25
-24
lines changed

src/libFLAC/bitreader.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "private/cpu.h"
4343
#include "private/macros.h"
4444
#include "FLAC/assert.h"
45+
#include "share/alloc.h"
4546
#include "share/compat.h"
4647
#include "share/endswap.h"
4748

@@ -255,7 +256,7 @@ static FLAC__bool bitreader_read_from_client_(FLAC__BitReader *br)
255256

256257
FLAC__BitReader *FLAC__bitreader_new(void)
257258
{
258-
FLAC__BitReader *br = calloc(1, sizeof(FLAC__BitReader));
259+
FLAC__BitReader *br = safe_calloc_(1, sizeof(FLAC__BitReader));
259260

260261
/* calloc() implies:
261262
memset(br, 0, sizeof(FLAC__BitReader));
@@ -290,7 +291,7 @@ FLAC__bool FLAC__bitreader_init(FLAC__BitReader *br, FLAC__BitReaderReadCallback
290291
br->words = br->bytes = 0;
291292
br->consumed_words = br->consumed_bits = 0;
292293
br->capacity = FLAC__BITREADER_DEFAULT_CAPACITY;
293-
br->buffer = malloc(sizeof(brword) * br->capacity);
294+
br->buffer = safe_malloc_(sizeof(brword) * br->capacity);
294295
if(br->buffer == 0)
295296
return false;
296297
br->read_callback = rcb;

src/libFLAC/bitwriter.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ FLAC__bool bitwriter_grow_(FLAC__BitWriter *bw, uint32_t bits_to_add)
156156

157157
FLAC__BitWriter *FLAC__bitwriter_new(void)
158158
{
159-
FLAC__BitWriter *bw = calloc(1, sizeof(FLAC__BitWriter));
159+
FLAC__BitWriter *bw = safe_calloc_(1, sizeof(FLAC__BitWriter));
160160
/* note that calloc() sets all members to 0 for us */
161161
return bw;
162162
}
@@ -181,7 +181,7 @@ FLAC__bool FLAC__bitwriter_init(FLAC__BitWriter *bw)
181181

182182
bw->words = bw->bits = 0;
183183
bw->capacity = FLAC__BITWRITER_DEFAULT_CAPACITY;
184-
bw->buffer = malloc(sizeof(bwword) * bw->capacity);
184+
bw->buffer = safe_malloc_(sizeof(bwword) * bw->capacity);
185185
if(bw->buffer == 0)
186186
return false;
187187

src/libFLAC/memory.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ FLAC__bool FLAC__memory_alloc_aligned_real_array(size_t elements, FLAC__real **u
212212
void *safe_malloc_mul_2op_p(size_t size1, size_t size2)
213213
{
214214
if(!size1 || !size2)
215-
return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
215+
return safe_malloc_(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
216216
if(size1 > SIZE_MAX / size2)
217217
return 0;
218-
return malloc(size1*size2);
218+
return safe_malloc_(size1*size2);
219219
}

src/libFLAC/metadata_iterators.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ FLAC_API const char * const FLAC__Metadata_SimpleIteratorStatusString[] = {
364364

365365
FLAC_API FLAC__Metadata_SimpleIterator *FLAC__metadata_simple_iterator_new(void)
366366
{
367-
FLAC__Metadata_SimpleIterator *iterator = calloc(1, sizeof(FLAC__Metadata_SimpleIterator));
367+
FLAC__Metadata_SimpleIterator *iterator = safe_calloc_(1, sizeof(FLAC__Metadata_SimpleIterator));
368368

369369
if(0 != iterator) {
370370
iterator->file = 0;
@@ -961,7 +961,7 @@ FLAC_API const char * const FLAC__Metadata_ChainStatusString[] = {
961961

962962
static FLAC__Metadata_Node *node_new_(void)
963963
{
964-
return calloc(1, sizeof(FLAC__Metadata_Node));
964+
return safe_calloc_(1, sizeof(FLAC__Metadata_Node));
965965
}
966966

967967
static void node_delete_(FLAC__Metadata_Node *node)
@@ -1559,7 +1559,7 @@ static FLAC__bool chain_rewrite_file_cb_(FLAC__Metadata_Chain *chain, FLAC__IOHa
15591559

15601560
FLAC_API FLAC__Metadata_Chain *FLAC__metadata_chain_new(void)
15611561
{
1562-
FLAC__Metadata_Chain *chain = calloc(1, sizeof(FLAC__Metadata_Chain));
1562+
FLAC__Metadata_Chain *chain = safe_calloc_(1, sizeof(FLAC__Metadata_Chain));
15631563

15641564
if(0 != chain)
15651565
chain_init_(chain);
@@ -1953,7 +1953,7 @@ FLAC_API void FLAC__metadata_chain_sort_padding(FLAC__Metadata_Chain *chain)
19531953

19541954
FLAC_API FLAC__Metadata_Iterator *FLAC__metadata_iterator_new(void)
19551955
{
1956-
FLAC__Metadata_Iterator *iterator = calloc(1, sizeof(FLAC__Metadata_Iterator));
1956+
FLAC__Metadata_Iterator *iterator = safe_calloc_(1, sizeof(FLAC__Metadata_Iterator));
19571957

19581958
/* calloc() implies:
19591959
iterator->current = 0;
@@ -2289,7 +2289,7 @@ FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_application_cb_(FLA
22892289
block->data = 0;
22902290
}
22912291
else {
2292-
if(0 == (block->data = malloc(block_length)))
2292+
if(0 == (block->data = safe_malloc_(block_length)))
22932293
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
22942294

22952295
if(read_cb(block->data, 1, block_length, handle) != block_length)
@@ -2390,7 +2390,7 @@ FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_cb_(
23902390
status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA;
23912391
goto skip;
23922392
}
2393-
else if(0 == (block->comments = calloc(block->num_comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) {
2393+
else if(0 == (block->comments = safe_calloc_(block->num_comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) {
23942394
block->num_comments = 0;
23952395
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
23962396
}
@@ -2460,7 +2460,7 @@ FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_track_cb_(
24602460
if(track->num_indices == 0) {
24612461
track->indices = 0;
24622462
}
2463-
else if(0 == (track->indices = calloc(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index))))
2463+
else if(0 == (track->indices = safe_calloc_(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index))))
24642464
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
24652465

24662466
for(i = 0; i < track->num_indices; i++) {
@@ -2520,7 +2520,7 @@ FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_cb_(FLAC__
25202520
if(block->num_tracks == 0) {
25212521
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA;
25222522
}
2523-
else if(0 == (block->tracks = calloc(block->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track))))
2523+
else if(0 == (block->tracks = safe_calloc_(block->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track))))
25242524
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
25252525

25262526
for(i = 0; i < block->num_tracks; i++) {
@@ -2626,7 +2626,7 @@ FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_unknown_cb_(FLAC__I
26262626
block->data = 0;
26272627
}
26282628
else {
2629-
if(0 == (block->data = malloc(block_length)))
2629+
if(0 == (block->data = safe_malloc_(block_length)))
26302630
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
26312631

26322632
if(read_cb(block->data, 1, block_length, handle) != block_length)

src/libFLAC/metadata_object.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_new(FLAC__MetadataType type
437437
if (type > FLAC__MAX_METADATA_TYPE)
438438
return 0;
439439

440-
object = calloc(1, sizeof(FLAC__StreamMetadata));
440+
object = safe_calloc_(1, sizeof(FLAC__StreamMetadata));
441441
if (object != NULL) {
442442
object->is_last = false;
443443
object->type = type;
@@ -958,7 +958,7 @@ FLAC_API FLAC__bool FLAC__metadata_object_seektable_resize_points(FLAC__StreamMe
958958
else {
959959
/* Leave object->data.seek_table.points untouched if realloc fails */
960960
FLAC__StreamMetadata_SeekPoint *tmpptr;
961-
if ((tmpptr = realloc(object->data.seek_table.points, new_size)) == NULL)
961+
if ((tmpptr = realloc_(object->data.seek_table.points, new_size)) == NULL)
962962
return false;
963963
object->data.seek_table.points = tmpptr;
964964
}
@@ -1217,7 +1217,7 @@ FLAC_API FLAC__bool FLAC__metadata_object_vorbiscomment_resize_comments(FLAC__St
12171217
else {
12181218
/* Leave object->data.vorbis_comment.comments untouched if realloc fails */
12191219
FLAC__StreamMetadata_VorbisComment_Entry *tmpptr;
1220-
if ((tmpptr = realloc(object->data.vorbis_comment.comments, new_size)) == NULL)
1220+
if ((tmpptr = realloc_(object->data.vorbis_comment.comments, new_size)) == NULL)
12211221
return false;
12221222
object->data.vorbis_comment.comments = tmpptr;
12231223
}
@@ -1465,7 +1465,7 @@ FLAC_API int FLAC__metadata_object_vorbiscomment_remove_entries_matching(FLAC__S
14651465

14661466
FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_new(void)
14671467
{
1468-
return calloc(1, sizeof(FLAC__StreamMetadata_CueSheet_Track));
1468+
return safe_calloc_(1, sizeof(FLAC__StreamMetadata_CueSheet_Track));
14691469
}
14701470

14711471
FLAC_API FLAC__StreamMetadata_CueSheet_Track *FLAC__metadata_object_cuesheet_track_clone(const FLAC__StreamMetadata_CueSheet_Track *object)
@@ -1533,7 +1533,7 @@ FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_resize_indices(FLAC__St
15331533
else {
15341534
/* Leave track->indices untouched if realloc fails */
15351535
FLAC__StreamMetadata_CueSheet_Index *tmpptr;
1536-
if ((tmpptr = realloc(track->indices, new_size)) == NULL)
1536+
if ((tmpptr = realloc_(track->indices, new_size)) == NULL)
15371537
return false;
15381538
track->indices = tmpptr;
15391539
}
@@ -1633,7 +1633,7 @@ FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_resize_tracks(FLAC__StreamMet
16331633
else {
16341634
/* Leave object->data.cue_sheet.tracks untouched if realloc fails */
16351635
FLAC__StreamMetadata_CueSheet_Track *tmpptr;
1636-
if ((tmpptr = realloc(object->data.cue_sheet.tracks, new_size)) == NULL)
1636+
if ((tmpptr = realloc_(object->data.cue_sheet.tracks, new_size)) == NULL)
16371637
return false;
16381638
object->data.cue_sheet.tracks = tmpptr;
16391639
}

src/libFLAC/stream_decoder.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new(void)
274274
}
275275

276276
decoder->private_->metadata_filter_ids_capacity = 16;
277-
if(0 == (decoder->private_->metadata_filter_ids = malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
277+
if(0 == (decoder->private_->metadata_filter_ids = safe_malloc_((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
278278
FLAC__bitreader_delete(decoder->private_->input);
279279
free(decoder->private_);
280280
free(decoder->protected_);
@@ -1467,7 +1467,7 @@ FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
14671467
case FLAC__METADATA_TYPE_APPLICATION:
14681468
/* remember, we read the ID already */
14691469
if(real_length > 0) {
1470-
if(0 == (block.data.application.data = malloc(real_length))) {
1470+
if(0 == (block.data.application.data = safe_malloc_(real_length))) {
14711471
decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
14721472
ok = false;
14731473
}
@@ -1495,7 +1495,7 @@ FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
14951495
break;
14961496
default:
14971497
if(real_length > 0) {
1498-
if(0 == (block.data.unknown.data = malloc(real_length))) {
1498+
if(0 == (block.data.unknown.data = safe_malloc_(real_length))) {
14991499
decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
15001500
ok = false;
15011501
}

0 commit comments

Comments
 (0)