Skip to content

Commit bd6b1ef

Browse files
Check return value of strdup calls (#862)
Albeit unlikely on modern server systems, memory allocation can still fail and the result, if unchecked, be null pointer dereferences. This adds a check to affected callsites of strdup to ensure allocation was successful, or abort if not.
1 parent 3de421d commit bd6b1ef

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

src/bin/pg_autoctl/debian.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,11 @@ buildDebianDataAndConfDirectoryNames(PostgresSetup *pgSetup,
282282
char clusterDir[MAXPGPATH] = { 0 };
283283
char versionDir[MAXPGPATH] = { 0 };
284284

285+
if (pgmajor == NULL)
286+
{
287+
log_error(ALLOCATION_FAILED_ERROR);
288+
return false;
289+
}
285290

286291
/* we need to work with the absolute pathname of PGDATA */
287292
if (!normalize_filename(pgSetup->pgdata, pgdata, MAXPGPATH))
@@ -301,6 +306,12 @@ buildDebianDataAndConfDirectoryNames(PostgresSetup *pgSetup,
301306
char *clusterDirName = strdup(basename(clusterDir));
302307
char *versionDirName = strdup(basename(versionDir));
303308

309+
if (clusterDirName == NULL || versionDirName == NULL)
310+
{
311+
log_error(ALLOCATION_FAILED_ERROR);
312+
return false;
313+
}
314+
304315
/* transform pgversion "11.4" to "11" to get the major version part */
305316
char *dot = strchr(pgmajor, '.');
306317

src/bin/pg_autoctl/ini_file.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,11 @@ ini_set_option_value(IniOption *option, const char *value)
252252
else
253253
{
254254
*(option->strValue) = strdup(value);
255+
if (*(option->strValue) == NULL)
256+
{
257+
log_error(ALLOCATION_FAILED_ERROR);
258+
return false;
259+
}
255260
}
256261
break;
257262
}
@@ -575,6 +580,11 @@ lookup_ini_path_value(IniOption *optionList, const char *path)
575580
}
576581

577582
section_name = strdup(path); /* don't scribble on path */
583+
if (section_name == NULL)
584+
{
585+
log_error(ALLOCATION_FAILED_ERROR);
586+
return NULL;
587+
}
578588
option_name = section_name + (ptr - path) + 1; /* apply same offset */
579589
*(option_name - 1) = '\0'; /* split string at the dot */
580590

@@ -629,6 +639,11 @@ ini_merge(IniOption *dstOptionList, IniOption *overrideOptionList)
629639
if (*(option->strValue) != NULL)
630640
{
631641
*(dstOption->strValue) = strdup(*(option->strValue));
642+
if (*(dstOption->strValue) == NULL)
643+
{
644+
log_error(ALLOCATION_FAILED_ERROR);
645+
return false;
646+
}
632647
}
633648
break;
634649
}

src/bin/pg_autoctl/pgsql.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ parseSingleValueResult(void *ctx, PGresult *result)
114114
case PGSQL_RESULT_STRING:
115115
{
116116
context->strVal = strdup(value);
117+
if (context->strVal == NULL)
118+
{
119+
context->parsedOk = false;
120+
log_error(ALLOCATION_FAILED_ERROR);
121+
}
117122
context->parsedOk = true;
118123
break;
119124
}

0 commit comments

Comments
 (0)