Skip to content

Commit 47b3776

Browse files
committed
Fix potential corruption bug when realloc fails
1 parent b861160 commit 47b3776

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

lib/lsof.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,8 @@ enum lsof_error lsof_select_process_regex(struct lsof_context *ctx, char *x) {
772772
MALLOC_S xl;
773773
char *xp = (char *)NULL;
774774
enum lsof_error ret = LSOF_SUCCESS;
775+
lsof_rx_t *new_rx;
776+
int new_cmdrx_cap;
775777

776778
if (!ctx || ctx->frozen) {
777779
return LSOF_ERROR_INVALID_ARGUMENT;
@@ -895,13 +897,13 @@ enum lsof_error lsof_select_process_regex(struct lsof_context *ctx, char *x) {
895897
/*
896898
* More CmdRx[] space must be assigned.
897899
*/
898-
NCmdRxA += 32;
899-
xl = (MALLOC_S)(ctx->cmd_regex_cap * sizeof(lsof_rx_t));
900+
new_cmdrx_cap = NCmdRxA + 32;
901+
xl = (MALLOC_S)(new_cmdrx_cap * sizeof(lsof_rx_t));
900902
if (CmdRx)
901-
CmdRx = (lsof_rx_t *)realloc((MALLOC_P *)CmdRx, xl);
903+
new_rx = (lsof_rx_t *)realloc((MALLOC_P *)CmdRx, xl);
902904
else
903-
CmdRx = (lsof_rx_t *)malloc(xl);
904-
if (!CmdRx) {
905+
new_rx = (lsof_rx_t *)malloc(xl);
906+
if (!new_rx) {
905907
if (ctx->err) {
906908
(void)fprintf(ctx->err, "%s: no space for regexp: ", Pn);
907909
safestrprt(x, ctx->err, 1);
@@ -910,9 +912,10 @@ enum lsof_error lsof_select_process_regex(struct lsof_context *ctx, char *x) {
910912
ret = LSOF_ERROR_NO_MEMORY;
911913
goto cleanup;
912914
}
915+
CmdRx = new_rx;
916+
NCmdRxA = new_cmdrx_cap;
913917
}
914918
i = NCmdRxU;
915-
CmdRx[i].exp = xp;
916919
/*
917920
* Compile the expression.
918921
*/

0 commit comments

Comments
 (0)