Skip to content

Commit

Permalink
Convert last snprintf and fscanf (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtuncer committed Mar 16, 2020
1 parent 13fbdff commit 1a74292
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
35 changes: 16 additions & 19 deletions src/bin/pg_autoctl/pgsetup.c
Expand Up @@ -403,13 +403,15 @@ pg_setup_init(PostgresSetup *pgSetup,
static bool
get_pgpid(PostgresSetup *pgSetup, bool pg_is_not_running_is_ok)
{
FILE *fp;
char *contents = NULL;
long fileSize = 0;
char pidfile[MAXPGPATH];
long pid = -1;
char *lines[1];
int pid = -1;

join_path_components(pidfile, pgSetup->pgdata, "postmaster.pid");

if ((fp = fopen_read_only(pidfile)) == NULL)
if (!read_file(pidfile, &contents, &fileSize))
{
if (!pg_is_not_running_is_ok)
{
Expand All @@ -419,39 +421,34 @@ get_pgpid(PostgresSetup *pgSetup, bool pg_is_not_running_is_ok)
return false;
}

if (fscanf(fp, "%ld", &pid) != 1)
if (fileSize == 0)
{
/* Is the file empty? */
if (ftell(fp) == 0 && feof(fp))
{
log_warn("The PID file \"%s\" is empty\n", pidfile);
}
else
{
log_warn("Invalid data in PID file \"%s\"\n", pidfile);
}
log_warn("The PID file \"%s\" is empty\n", pidfile);
}
else if (splitLines(contents, lines, 1) != 1 ||
!stringToInt(lines[0], &pid))
{
log_warn("Invalid data in PID file \"%s\"\n", pidfile);
}

fclose(fp);

if (pid > 0 && pid <= INT_MAX)
{
if (kill(pid, 0) == 0)
{
pgSetup->pidFile.pid = pid;

log_trace("get_pgpid: %ld", pid);
log_trace("get_pgpid: %d", pid);
return true;
}
else
{
if (!pg_is_not_running_is_ok)
{
log_warn("Read a stale pid in \"postmaster.pid\": %ld", pid);
log_warn("Read a stale pid in \"postmaster.pid\": %d", pid);
}
else
{
log_debug("Read a stale pid in \"postmaster.pid\": %ld", pid);
log_debug("Read a stale pid in \"postmaster.pid\": %d", pid);
}

return false;
Expand All @@ -460,7 +457,7 @@ get_pgpid(PostgresSetup *pgSetup, bool pg_is_not_running_is_ok)
else
{
/* that's more like a bug, really */
log_error("Invalid PID \"%ld\" read in \"postmaster.pid\"", pid);
log_error("Invalid PID \"%d\" read in \"postmaster.pid\"", pid);
return false;
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/monitor/health_check_worker.c
Expand Up @@ -795,12 +795,12 @@ ManageHealthCheck(HealthCheck *healthCheck, struct timeval currentTime)
{
PGconn *connection = NULL;
ConnStatusType connStatus = CONNECTION_BAD;
char connInfoString[MAX_CONN_INFO_SIZE];
StringInfo connInfoString = makeStringInfo();

snprintf(connInfoString, MAX_CONN_INFO_SIZE, CONN_INFO_TEMPLATE,
appendStringInfo(connInfoString, CONN_INFO_TEMPLATE,
nodeHealth->nodeName, nodeHealth->nodePort, HealthCheckTimeout);

connection = PQconnectStart(connInfoString);
connection = PQconnectStart(connInfoString->data);
PQsetnonblocking(connection, true);

connStatus = PQstatus(connection);
Expand Down Expand Up @@ -831,6 +831,9 @@ ManageHealthCheck(HealthCheck *healthCheck, struct timeval currentTime)

healthCheck->numTries++;

pfree(connInfoString->data);
pfree(connInfoString);

break;
}

Expand Down
8 changes: 7 additions & 1 deletion src/monitor/notifications.c
Expand Up @@ -36,7 +36,13 @@ LogAndNotifyMessage(char *message, size_t size, const char *fmt, ...)
va_list args;

va_start(args, fmt);
n = vsnprintf(message, size-2, fmt, args);
/*
* Explanation of IGNORE-BANNED
* Arguments are always non-null and we
* do not write before the allocated buffer.
*
*/
n = vsnprintf(message, size-2, fmt, args); /* IGNORE-BANNED */
va_end(args);

if (n < 0)
Expand Down

0 comments on commit 1a74292

Please sign in to comment.