Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve code according to the static analyzer report #821

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/bin/lib/log/src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ void log_log(int level, const char *file, int line, const char *fmt, ...)
gettimeofday(&t, NULL);
lt = localtime(&t.tv_sec);

if (!lt) {
return;
}

char *json_string = NULL;

/* Prepare JSON format if needed */
Expand Down
2 changes: 2 additions & 0 deletions src/bin/lib/parson/parson.c
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,8 @@ static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, int le
}
if (parson_number_serialization_function) {
written = parson_number_serialization_function(num, num_buf);
} else if (!num_buf) {
return -1;
} else if (parson_float_format) {
written = snprintf(num_buf, PARSON_NUM_BUF_SIZE, parson_float_format, num);
} else {
Expand Down
6 changes: 6 additions & 0 deletions src/bin/lib/subcommands.c/runprogram.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,12 @@ execute_program(Program *prog)
*/
int stdIn = open(DEV_NULL, O_RDONLY);

if (stdIn < 0) {
prog->returnCode = -1;
prog->error = errno;
return;
}

/* Avoid double-output problems */
fflush(stdout);
fflush(stderr);
Expand Down
9 changes: 9 additions & 0 deletions src/bin/pgcopydb/cli_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,8 @@ cli_read_previous_options(CopyDBOptions *options, CopyFilePaths *cfPaths)
val,
opts[i].size))
{
free(val);

/* errors have already been logged */
return false;
}
Expand All @@ -713,6 +715,8 @@ cli_read_previous_options(CopyDBOptions *options, CopyFilePaths *cfPaths)
opts[i].optname,
opts[i].target,
opts[i].filename);

free(val);
}

/*
Expand All @@ -728,8 +732,13 @@ cli_read_previous_options(CopyDBOptions *options, CopyFilePaths *cfPaths)
val,
opts[i].optname,
opts[i].target);

free(val);

return false;
}

free(val);
}

/*
Expand Down
2 changes: 1 addition & 1 deletion src/bin/pgcopydb/ld_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -2760,7 +2760,7 @@ stream_read_context(CDCPaths *paths,
sleepTimeMs);

/* we have milliseconds, pg_usleep() wants microseconds */
(void) pg_usleep(sleepTimeMs * 1000);
(void) pg_usleep(sleepTimeMs * 1000L);
}

/* did retry policy expire before the files are created? */
Expand Down
7 changes: 7 additions & 0 deletions src/bin/pgcopydb/parsing_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@ buildPostgresURIfromPieces(URIParams *uriParams, char **pguri)
"value \"%s\"",
keyword, value);
}
destroyPQExpBuffer(uri);
return false;
}

Expand Down Expand Up @@ -925,6 +926,9 @@ escapeWithPercentEncoding(const char *str, char **dst)
log_error("BUG: percent-encoded Postgres URI does not fit "
"in the computed size: %lld bytes",
(long long) size);

free(escaped);

return false;
}

Expand All @@ -946,6 +950,9 @@ escapeWithPercentEncoding(const char *str, char **dst)
log_error("BUG: percent-encoded Postgres URI does not fit "
"in the computed size: %lld bytes",
(long long) size);

free(escaped);

return false;
}

Expand Down
5 changes: 5 additions & 0 deletions src/bin/pgcopydb/pgcmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1502,12 +1502,17 @@ tokenize_archive_list_entry(ArchiveToken *token)
{
log_error("Failed to parse OID \"%s\" from pg_restore --list",
buf);

free(buf);

return false;
}

token->type = ARCHIVE_TOKEN_OID;
token->ptr = ptr;

free(buf);

return true;
}

Expand Down
9 changes: 5 additions & 4 deletions src/bin/pgcopydb/pgsql.c
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ pgsql_retry_open_connection(PGSQL *pgsql)
pgsql_compute_connection_retry_sleep_time(&(pgsql->retryPolicy));

/* we have milliseconds, pg_usleep() wants microseconds */
(void) pg_usleep(sleep * 1000);
(void) pg_usleep(sleep * 1000L);

log_sql("PQping(%s): slept %d ms on attempt %d",
pgsql->safeURI.pguri,
Expand Down Expand Up @@ -831,7 +831,7 @@ pgsql_retry_open_connection(PGSQL *pgsql)
}
}

if (!connectionOk && pgsql->connection != NULL)
if (!connectionOk || pgsql->connection != NULL)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this change here is a mistake.

{
INSTR_TIME_SET_CURRENT(pgsql->retryPolicy.connectTime);

Expand Down Expand Up @@ -2450,6 +2450,7 @@ build_parameters_list(PQExpBuffer buffer,
log_error("Failed to create log message for SQL query parameters: "
"out of memory");
destroyPQExpBuffer(buffer);
buffer = NULL;
return false;
}
}
Expand Down Expand Up @@ -3828,8 +3829,8 @@ pg_copy_large_object(PGSQL *src,
* 4. Read the large object content in chunks on the source
* database, and write them on the target database.
*/
uint64_t bytesRead = 0;
uint64_t bytesWritten = 0;
int64_t bytesRead = 0;
int64_t bytesWritten = 0;

do {
char *buffer = (char *) calloc(LOBBUFSIZE, sizeof(char));
Expand Down
2 changes: 1 addition & 1 deletion src/bin/pgcopydb/schema.c
Original file line number Diff line number Diff line change
Expand Up @@ -4243,7 +4243,7 @@ getExtensionList(void *ctx, PGresult *result)
}

/* now loop over extension configuration, if any */
if (extension->config.count > 0)
if (extension && extension->config.count > 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really necessary as we early exit if extension is null.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static analyzer didn't catch it

{
SourceExtensionConfig *config =
(SourceExtensionConfig *) calloc(1, sizeof(SourceExtensionConfig));
Expand Down
14 changes: 9 additions & 5 deletions src/bin/pgcopydb/string_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,12 @@ stringToInt64(const char *str, int64_t *number)
{
return false;
}
#if LLONG_MAX > INT64_MAX
else if (n < INT64_MIN || n > INT64_MAX)
{
return false;
}
#endif

*number = n;

Expand Down Expand Up @@ -208,10 +210,12 @@ stringToUInt64(const char *str, uint64_t *number)
{
return false;
}
#if LLONG_MAX > INT64_MAX
else if (n > UINT64_MAX)
{
return false;
}
#endif

*number = n;

Expand Down Expand Up @@ -507,7 +511,7 @@ IntervalToString(uint64_t millisecs, char *buffer, size_t size)
else if (seconds < 10.0)
{
int s = (int) seconds;
uint64_t ms = millisecs - (1000 * s);
uint64_t ms = millisecs - (1000L * s);

sformat(buffer, size, "%2ds%03lld", s, (long long) ms);
}
Expand Down Expand Up @@ -692,7 +696,7 @@ processBufferCallback(const char *buffer, bool error)
void
pretty_print_bytes(char *buffer, size_t size, uint64_t bytes)
{
const char *suffixes[7] = {
const char *suffixes[] = {
"B", /* Bytes */
"kB", /* Kilo */
"MB", /* Mega */
Expand All @@ -705,7 +709,7 @@ pretty_print_bytes(char *buffer, size_t size, uint64_t bytes)
uint sIndex = 0;
long double count = bytes;

while (count >= 10240 && sIndex < 7)
while (count >= 10240 && sIndex < (sizeof(suffixes) / sizeof(char) - 1))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can understand why we want to remove hard coded array sizes. However, I have some questions:

  1. Why do you need the -1?
  2. Why did you divide by sizeof(char) where elements are actually char*?

{
sIndex++;
count /= 1024;
Expand Down Expand Up @@ -766,7 +770,7 @@ pretty_print_bytes_per_second(char *buffer, size_t size, uint64_t bytes,
void
pretty_print_count(char *buffer, size_t size, uint64_t number)
{
const char *suffixes[7] = {
const char *suffixes[] = {
"", /* units */
"thousands", /* 10^3 */
"million", /* 10^6 */
Expand All @@ -793,7 +797,7 @@ pretty_print_count(char *buffer, size_t size, uint64_t number)
long double count = number;

/* issue 1234 million rather than 1 billion or 1.23 billion */
while (count >= 10000 && sIndex < 7)
while (count >= 10000 && sIndex < (sizeof(suffixes) / sizeof(char) - 1))
{
sIndex++;
count /= 1000;
Expand Down