Skip to content

Commit

Permalink
Keep avifRWDataRealloc() input unchanged on error
Browse files Browse the repository at this point in the history
Free resources in aviftest.c.
Change TODO syntax to match style guide.
  • Loading branch information
y-guyon committed Jul 27, 2023
1 parent fa89874 commit 5b2741a
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 27 deletions.
6 changes: 2 additions & 4 deletions apps/shared/iccmaker.c
Original file line number Diff line number Diff line change
Expand Up @@ -444,10 +444,9 @@ avifBool avifGenerateRGBICC(avifRWData * icc, float gamma, const float primaries
}

computeMD5(buffer, sizeof(iccColorTemplate));
if (avifRWDataRealloc(icc, iccColorLength) != AVIF_RESULT_OK) {
if (avifRWDataSet(icc, buffer, iccColorLength) != AVIF_RESULT_OK) {
return AVIF_FALSE;
}
memcpy(icc->data, buffer, iccColorLength);

return AVIF_TRUE;
}
Expand All @@ -471,10 +470,9 @@ avifBool avifGenerateGrayICC(avifRWData * icc, float gamma, const float white[2]
}

computeMD5(buffer, sizeof(iccGrayTemplate));
if (avifRWDataRealloc(icc, iccGrayLength) != AVIF_RESULT_OK) {
if (avifRWDataSet(icc, buffer, iccGrayLength) != AVIF_RESULT_OK) {
return AVIF_FALSE;
}
memcpy(icc->data, buffer, iccGrayLength);

return AVIF_TRUE;
}
1 change: 1 addition & 0 deletions include/avif/avif.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ typedef struct avifRWData
// clang-format on

// The avifRWData input must be zero-initialized before being manipulated with these functions.
// If AVIF_RESULT_OUT_OF_MEMORY is returned, raw is left unchanged.
AVIF_API avifResult avifRWDataRealloc(avifRWData * raw, size_t newSize);
AVIF_API avifResult avifRWDataSet(avifRWData * raw, const uint8_t * data, size_t len);
AVIF_API void avifRWDataFree(avifRWData * raw);
Expand Down
3 changes: 1 addition & 2 deletions src/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ void * avifAlloc(size_t size)
{
void * out = malloc(size);
if (out == NULL) {
// TODO: https://github.com/AOMediaCodec/libavif/issues/820
// - Remove once all calling sites propagate the error as AVIF_RESULT_OUT_OF_MEMORY.
// TODO(issue #820): Remove once all calling sites propagate the error as AVIF_RESULT_OUT_OF_MEMORY.
abort();
}
return out;
Expand Down
21 changes: 6 additions & 15 deletions src/rawdata.c
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
// Copyright 2019 Joe Drago. All rights reserved.
// SPDX-License-Identifier: BSD-2-Clause

#include "avif/avif.h"
#include "avif/internal.h"

#include <string.h>

avifResult avifRWDataRealloc(avifRWData * raw, size_t newSize)
{
if (raw->size != newSize) {
uint8_t * old = raw->data;
size_t oldSize = raw->size;
raw->data = avifAlloc(newSize);
if (!raw->data) {
// The alternative would be to keep old in raw->data but this avoids
// the need of calling avifRWDataFree() on error.
avifFree(old);
raw->size = 0;
return AVIF_RESULT_OUT_OF_MEMORY;
uint8_t * newData = avifAlloc(newSize);
AVIF_CHECKERR(newData, AVIF_RESULT_OUT_OF_MEMORY);
if (raw->size && newSize) {
memcpy(newData, raw->data, AVIF_MIN(raw->size, newSize));
}
avifFree(raw->data);
raw->data = newData;
raw->size = newSize;
if (oldSize) {
size_t bytesToCopy = (oldSize < raw->size) ? oldSize : raw->size;
memcpy(raw->data, old, bytesToCopy);
avifFree(old);
}
}
return AVIF_RESULT_OK;
}
Expand Down
3 changes: 1 addition & 2 deletions src/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,7 @@ static void makeRoom(avifRWStream * stream, size_t size)
newSize += AVIF_STREAM_BUFFER_INCREMENT;
}
if (avifRWDataRealloc(stream->raw, newSize) != AVIF_RESULT_OK) {
// TODO: https://github.com/AOMediaCodec/libavif/issues/820
// - Return AVIF_RESULT_OUT_OF_MEMORY instead.
// TODO(issue #820): Return AVIF_RESULT_OUT_OF_MEMORY instead.
abort();
}
}
Expand Down
13 changes: 9 additions & 4 deletions tests/aviftest.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,27 +187,32 @@ static int runIOTests(const char * dataDir)
size_t filenameLen = strlen(filename);
if ((ioDirLen + filenameLen) > FILENAME_MAX_LENGTH) {
printf("Path too long: %s\n", filename);
return 1;
retCode = 1;
break;
}
strcpy(fullFilename, ioDir);
strcat(fullFilename, filename);

FILE * f = fopen(fullFilename, "rb");
if (!f) {
printf("Can't open for read: %s\n", filename);
return 1;
retCode = 1;
break;
}
fseek(f, 0, SEEK_END);
size_t fileSize = ftell(f);
fseek(f, 0, SEEK_SET);
if (avifRWDataRealloc(&fileBuffer, fileSize) != AVIF_RESULT_OK) {
printf("Out of memory when allocating buffer to read file: %s\n", filename);
return 1;
fclose(f);
retCode = 1;
break;
}
if (fread(fileBuffer.data, 1, fileSize, f) != fileSize) {
printf("Can't read entire file: %s\n", filename);
fclose(f);
return 1;
retCode = 1;
break;
}
fclose(f);

Expand Down

0 comments on commit 5b2741a

Please sign in to comment.