commit dba19fd487cdf9eca04acf58e66f4cd930f8f9ae
parent 9a785114268b2bbfd559a60705561b78864870f1
Author: Morel BĂ©renger <berengermorel76@gmail.com>
Date: Sat, 30 May 2020 02:19:56 +0200
fix potential memory leak
Diffstat:
1 file changed, 11 insertions(+), 1 deletion(-)
diff --git a/lmerge.cpp b/lmerge.cpp
@@ -128,12 +128,19 @@ int main( void )
while( !buf && buf_sz >= 32 )
{
buf_sz /= 2;
- buf = static_cast<char*>( malloc( buf_sz ) );
+ char* nbuf = static_cast<char*>( realloc( buf, buf_sz ) );
+ if( !nbuf )
+ {
+ free( buf );
+ return EXIT_FAILURE;
+ }
+ buf = nbuf;
}
if( !buf )
{
fprintf( stderr, "ERROR: malloc %s(%d)\n", strerror( errno ), errno );
+ free( buf );
return EXIT_FAILURE;
}
@@ -150,6 +157,7 @@ int main( void )
if( !feof( stdin ) )
{
fprintf( stderr, "ERROR: fgets %s(%d)\n", strerror( errno ), errno );
+ free( buf );
return EXIT_FAILURE;
}
break;
@@ -159,6 +167,7 @@ int main( void )
if( str_sz == buf_sz - 1 && buf[str_sz] != '\n' && !feof( stdin ) )
{
fprintf( stderr, "ERROR: buffer too small for some lines\n" );
+ free( buf );
return EXIT_FAILURE;
}
@@ -251,6 +260,7 @@ int main( void )
fputs( last_line.data(), stdout );
}
fputc( *SEP_ENTRY, stdout );
+ free( buf );
return EXIT_SUCCESS;
}