1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-29 15:23:05 +04:00

Merge pull request #24291 from visitorckw:fix-memory-leak

Fix memory leak and handle realloc failure
This commit is contained in:
Alexander Smorkalov
2023-09-20 08:49:56 +03:00
committed by GitHub
+18 -6
View File
@@ -375,15 +375,21 @@ static long THDiskFile_readString(THFile *self, const char *format, char **str_)
long total = TBRS_BSZ;
long pos = 0L;
if (p == NULL)
THError("read error: failed to allocate buffer");
for (;;)
{
if(total-pos == 0) /* we need more space! */
{
total += TBRS_BSZ;
p = (char*)THRealloc(p, total);
char *new_p = (char*)THRealloc(p, total);
if (new_p == NULL)
{
THFree(p);
THError("read error: failed to reallocate buffer");
}
p = new_p;
}
if (p == NULL)
THError("read error: failed to allocate buffer");
pos += fread(p+pos, 1, total-pos, dfself->handle);
if (pos < total) /* eof? */
{
@@ -409,15 +415,21 @@ static long THDiskFile_readString(THFile *self, const char *format, char **str_)
long pos = 0L;
long size;
if (p == NULL)
THError("read error: failed to allocate buffer");
for (;;)
{
if(total-pos <= 1) /* we can only write '\0' in there! */
{
total += TBRS_BSZ;
p = (char*)THRealloc(p, total);
char *new_p = (char*)THRealloc(p, total);
if (new_p == NULL)
{
THFree(p);
THError("read error: failed to reallocate buffer");
}
p = new_p;
}
if (p == NULL)
THError("read error: failed to allocate buffer");
if (fgets(p+pos, total-pos, dfself->handle) == NULL) /* eof? */
{
if(pos == 0L)