Skip to content

Commit 1777da9

Browse files
committed
REVIEWED: Avoid realloc() calls, small security improvement
1 parent 6226abb commit 1777da9

File tree

4 files changed

+39
-25
lines changed

4 files changed

+39
-25
lines changed

src/rcore.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2562,19 +2562,20 @@ unsigned char *DecompressData(const unsigned char *compData, int compDataSize, i
25622562

25632563
#if defined(SUPPORT_COMPRESSION_API)
25642564
// Decompress data from a valid DEFLATE stream
2565-
data = (unsigned char *)RL_CALLOC(MAX_DECOMPRESSION_SIZE*1024*1024, 1);
2565+
unsigned char *data0 = (unsigned char *)RL_CALLOC(MAX_DECOMPRESSION_SIZE*1024*1024, 1);
25662566
int length = sinflate(data, MAX_DECOMPRESSION_SIZE*1024*1024, compData, compDataSize);
25672567

2568-
// WARNING: RL_REALLOC can make (and leave) data copies in memory, be careful with sensitive compressed data!
2569-
// TODO: Use a different approach, create another buffer, copy data manually to it and wipe original buffer memory
2570-
unsigned char *temp = (unsigned char *)RL_REALLOC(data, length);
2571-
2572-
if (temp != NULL) data = temp;
2573-
else TRACELOG(LOG_WARNING, "SYSTEM: Failed to re-allocate required decompression memory");
2568+
// WARNING: RL_REALLOC can make (and leave) data copies in memory,
2569+
// that can be a security concern in case of compression of sensitive data
2570+
// So, we use a second buffer to copy data manually, wiping original buffer memory
2571+
data = (unsigned char *)RL_CALLOC(length, 1);
2572+
memcpy(data, data0, length);
2573+
memset(data0, 0, MAX_DECOMPRESSION_SIZE*1024*1024); // Wipe memory, is memset() safe?
2574+
RL_FREE(data0);
2575+
2576+
TRACELOG(LOG_INFO, "SYSTEM: Decompress data: Comp. size: %i -> Original size: %i", compDataSize, length);
25742577

25752578
*dataSize = length;
2576-
2577-
TRACELOG(LOG_INFO, "SYSTEM: Decompress data: Comp. size: %i -> Original size: %i", compDataSize, *dataSize);
25782579
#endif
25792580

25802581
return data;

src/rmodels.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6573,13 +6573,23 @@ static Model LoadM3D(const char *fileName)
65736573
// Materials are grouped together
65746574
if (mi != m3d->face[i].materialid)
65756575
{
6576-
// there should be only one material switch per material kind, but be bulletproof for non-optimal model files
6576+
// There should be only one material switch per material kind,
6577+
// but be bulletproof for non-optimal model files
65776578
if (k + 1 >= model.meshCount)
65786579
{
65796580
model.meshCount++;
6580-
model.meshes = (Mesh *)RL_REALLOC(model.meshes, model.meshCount*sizeof(Mesh));
6581-
memset(&model.meshes[model.meshCount - 1], 0, sizeof(Mesh));
6582-
model.meshMaterial = (int *)RL_REALLOC(model.meshMaterial, model.meshCount*sizeof(int));
6581+
6582+
// Create a second buffer for mesh re-allocation
6583+
Mesh *tempMeshes = (Mesh *)RL_CALLOC(model.meshCount, sizeof(Mesh));
6584+
memcpy(tempMeshes, model.meshes, (model.meshCount - 1)*sizeof(Mesh));
6585+
RL_FREE(model.meshes);
6586+
model.meshes = tempMeshes;
6587+
6588+
// Create a second buffer for material re-allocation
6589+
int *tempMeshMaterial = (int *)RL_CALLOC(model.meshCount, sizeof(int));
6590+
memcpy(tempMeshMaterial, model.meshMaterial, (model.meshCount - 1)*sizeof(int));
6591+
RL_FREE(model.meshMaterial);
6592+
model.meshMaterial = tempMeshMaterial;
65836593
}
65846594

65856595
k++;

src/rtext.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,10 +1920,11 @@ char *LoadUTF8(const int *codepoints, int length)
19201920
size += bytes;
19211921
}
19221922

1923-
// Resize memory to text length + string NULL terminator
1924-
void *ptr = RL_REALLOC(text, size + 1);
1925-
1926-
if (ptr != NULL) text = (char *)ptr;
1923+
// Create second buffer and copy data manually to it
1924+
char *temp = (char *)RL_CALLOC(size + 1, 1);
1925+
memcpy(temp, text, size);
1926+
RL_FREE(text);
1927+
text = temp;
19271928

19281929
return text;
19291930
}
@@ -1951,8 +1952,11 @@ int *LoadCodepoints(const char *text, int *count)
19511952
i += codepointSize;
19521953
}
19531954

1954-
// Re-allocate buffer to the actual number of codepoints loaded
1955-
codepoints = (int *)RL_REALLOC(codepoints, codepointCount*sizeof(int));
1955+
// Create second buffer and copy data manually to it
1956+
int *temp = (int *)RL_CALLOC(codepointCount, sizeof(int));
1957+
for (int i = 0; i < codepointCount; i++) temp[i] = codepoints[i];
1958+
RL_FREE(codepoints);
1959+
codepoints = temp;
19561960

19571961
*count = codepointCount;
19581962

src/rtextures.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,10 +2400,11 @@ void ImageMipmaps(Image *image)
24002400

24012401
if (image->mipmaps < mipCount)
24022402
{
2403-
void *temp = RL_REALLOC(image->data, mipSize);
2404-
2405-
if (temp != NULL) image->data = temp; // Assign new pointer (new size) to store mipmaps data
2406-
else TRACELOG(LOG_WARNING, "IMAGE: Mipmaps required memory could not be allocated");
2403+
// Create second buffer and copy data manually to it
2404+
void *temp = RL_CALLOC(mipSize, 1);
2405+
memcpy(temp, image->data, GetPixelDataSize(image->width, image->height, image->format));
2406+
RL_FREE(image->data);
2407+
image->data = temp;
24072408

24082409
// Pointer to allocated memory point where store next mipmap level data
24092410
unsigned char *nextmip = image->data;
@@ -2429,9 +2430,7 @@ void ImageMipmaps(Image *image)
24292430
if (i < image->mipmaps) continue;
24302431

24312432
TRACELOGD("IMAGE: Generating mipmap level: %i (%i x %i) - size: %i - offset: 0x%x", i, mipWidth, mipHeight, mipSize, nextmip);
2432-
24332433
ImageResize(&imCopy, mipWidth, mipHeight); // Uses internally Mitchell cubic downscale filter
2434-
24352434
memcpy(nextmip, imCopy.data, mipSize);
24362435
}
24372436

0 commit comments

Comments
 (0)