Commit 43004384 authored by St4lkerino's avatar St4lkerino Committed by Ludovic Rousseau

Fix realloc(3) error handling

From realloc(3) manpage:
       The realloc() function returns a pointer to the newly allocated memory,
       which  is  suitably  aligned for any built-in type and may be different
       from ptr, or NULL if the request fails.  If size was equal to 0, either
       NULL  or  a  pointer  suitable  to be passed to free() is returned.  If
       realloc() fails, the original block is left untouched; it is not  freed
       or moved.

If realloc() fails then the memory that was previously allocated needs
to be freed, or it will create a memory leak.

It was not a real problem because if realloc(3) failed then pcscd would
exit immediatly and the memory would not leak for a long time.

Thanks to PA193 project
https://github.com/vegaMato/PCSC-lite-project-tasks
parent ad8c6fb9
......@@ -228,15 +228,19 @@ static LONG HPReadBundleValues(void)
Log2(PCSC_LOG_INFO,
"Increase driverTracker to %d entries", driverSize);
#endif
driverTracker = realloc(driverTracker,
void* tmp = realloc(driverTracker,
driverSize * sizeof(*driverTracker));
if (NULL == driverTracker)
if (NULL == tmp)
{
free(driverTracker);
Log1(PCSC_LOG_CRITICAL, "Not enough memory");
driverSize = -1;
(void)closedir(hpDir);
return -1;
}
driverTracker = tmp;
/* clean the newly allocated entries */
for (i=driverSize-DRIVER_TRACKER_SIZE_STEP; i<driverSize; i++)
......
......@@ -234,15 +234,17 @@ static LONG HPReadBundleValues(void)
Log2(PCSC_LOG_INFO,
"Increase driverTracker to %d entries", driverSize);
#endif
driverTracker = realloc(driverTracker,
void* tmp = realloc(driverTracker,
driverSize * sizeof(*driverTracker));
if (NULL == driverTracker)
if (NULL == tmp)
{
free(driverTracker);
Log1(PCSC_LOG_CRITICAL, "Not enough memory");
driverSize = -1;
closedir(hpDir);
return -1;
}
driverTracker = tmp;
/* clean the newly allocated entries */
for (i=driverSize-DRIVER_TRACKER_SIZE_STEP; i<driverSize; i++)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment