hackorama/AnyOption — Issue #2: [PATCH][cppcheck] fixed 6 possible memory leaks 
Author: orbitcowboy
Created: 2011-10-13 12:37:07+00:00
State: open

scanning the current git head with cppcheck ( a static code analysis), it found 6 possible memory leaks. The tools printed the following warings:

$ cppcheck -v .
Checking anyoption.cpp...
[anyoption.cpp:197]: (error) Common realloc mistake: 'optiontype' nulled but not freed upon failure
[anyoption.cpp:199]: (error) Common realloc mistake: 'optionindex' nulled but not freed upon failure
[anyoption.cpp:216]: (error) Common realloc mistake: 'optionchars' nulled but not freed upon failure
[anyoption.cpp:218]: (error) Common realloc mistake: 'optchartype' nulled but not freed upon failure
[anyoption.cpp:220]: (error) Common realloc mistake: 'optcharindex' nulled but not freed upon failure

Please refer the attached patch, that fixes the issue:

diff --git a/anyoption.cpp b/anyoption.cpp
index 9b334a6..dcb31db 100755
--- a/anyoption.cpp
+++ b/anyoption.cpp
@@ -192,14 +192,38 @@ AnyOption::alloc()
bool
AnyOption::doubleOptStorage()
{
+ // create a backup of options pointer
+ const char **optionsBackup = options;
options = (const char**)realloc( options,
((2*max_options)+1) * sizeof( const char*) );
+ // if realloc has failed, options will point to NULL --> free the memory
+ if( options == NULL )
+ {
+ free(optionsBackup);
+ return false;
+ }
+
+ // create a backup of pointer
+ int * optiontypeBackup = optiontype;
optiontype = (int*) realloc( optiontype ,
-	((2 * max_options)+1)* sizeof(int) );
+	((2 * max_options)+1)* sizeof(int) );
+ // if realloc has failed, options will point to NULL --> free the memory
+ if(optiontype == NULL)
+ {
+ free(optiontypeBackup);
+ return false;
+ }
+
+ // create a backup of pointer
+ int * optionindexBackup = optionindex;
optionindex = (int*) realloc( optionindex,
((2 * max_options)+1) * sizeof(int) );
-	if( options == NULL || optiontype == NULL || optionindex == NULL )
+ // if realloc has failed, options will point to NULL --> free the memory
+	if( optionindex == NULL )
+ {
+ free(optionindexBackup);
return false;
+ }
/* init new storage */
for( int i = max_options ; i < 2*max_options ; i++ ){
options[i] = NULL;
@@ -213,16 +237,39 @@ AnyOption::doubleOptStorage()
bool
AnyOption::doubleCharStorage()
{
+ // create a backup of pointer
+ char * optioncharsBackup = optionchars;
optionchars = (char*) realloc( optionchars,
((2*max_char_options)+1)*sizeof(char) );
+ // if realloc has failed, options will point to NULL --> free the memory
+ if(optionchars == NULL)
+ {
+ free(optioncharsBackup);
+ return false;
+ }
+
+ // create a backup of pointer
+ int * optchartypeBackup = optchartype;
optchartype = (int*) realloc( optchartype,
-	((2*max_char_options)+1)*sizeof(int) );
+	((2*max_char_options)+1)*sizeof(int) );
+ // if realloc has failed, options will point to NULL --> free the memory
+ if(optchartype == NULL)
+ {
+ free(optchartypeBackup);
+ return false;
+ }
+
+ // create a backup of pointer
+ int * optcharindexBackup = optcharindex;
optcharindex = (int*) realloc( optcharindex,
((2*max_char_options)+1)*sizeof(int) );
-	if( optionchars == NULL ||
-	optchartype == NULL ||
-	optcharindex == NULL )
-	return false;
+ // if realloc has failed, options will point to NULL --> free the memory
+ if(optcharindex == NULL)
+ {
+ free(optcharindexBackup);
+ return false;
+ }
+
/* init new storage */
for( int i = max_char_options ; i < 2*max_char_options ; i++ ){
optionchars[i] = '0';

Best regards from the cppcheck team

Ettl Martin