Skip to content

Commit

Permalink
Fix memory leak on overflow in _php_stream_scandir()
Browse files Browse the repository at this point in the history
On overflow, only the array is freed, but not the strings.
  • Loading branch information
nielsdos committed Feb 13, 2025
1 parent a54af45 commit 7a55ea0
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions main/streams/streams.c
Original file line number Diff line number Diff line change
Expand Up @@ -2469,24 +2469,18 @@ PHPAPI int _php_stream_scandir(const char *dirname, zend_string **namelist[], in
vector_size = 10;
} else {
if(vector_size*2 < vector_size) {
/* overflow */
php_stream_closedir(stream);
efree(vector);
return -1;
goto overflow;
}
vector_size *= 2;
}
vector = (zend_string **) safe_erealloc(vector, vector_size, sizeof(char *), 0);
vector = (zend_string **) safe_erealloc(vector, vector_size, sizeof(zend_string *), 0);
}

vector[nfiles] = zend_string_init(sdp.d_name, strlen(sdp.d_name), 0);

nfiles++;
if(vector_size < 10 || nfiles == 0) {
/* overflow */
php_stream_closedir(stream);
efree(vector);
return -1;
goto overflow;
}
}
php_stream_closedir(stream);
Expand All @@ -2497,5 +2491,13 @@ PHPAPI int _php_stream_scandir(const char *dirname, zend_string **namelist[], in
qsort(*namelist, nfiles, sizeof(zend_string *), (int(*)(const void *, const void *))compare);
}
return nfiles;

overflow:
php_stream_closedir(stream);
for (unsigned int i = 0; i < nfiles; i++) {
zend_string_efree(vector[i]);
}
efree(vector);
return -1;
}
/* }}} */

0 comments on commit 7a55ea0

Please sign in to comment.