Skip to content

Commit

Permalink
avoid usage of snprintf()
Browse files Browse the repository at this point in the history
We have a nice little abstraction for snprintf with covering the case
that a string is too big for the target buffer – let's use that!
  • Loading branch information
Christian Kruse committed Mar 6, 2014
1 parent 164cf9d commit 9eba986
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
12 changes: 4 additions & 8 deletions log.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@
/* #define REPMGR_DEBUG */

void
stderr_log_with_level(const char *level_name, int level, const char *fmt,...)
stderr_log_with_level(const char *level_name, int level, const char *fmt, ...)
{
size_t len = strlen(fmt);
char fmt1[len + 150];
time_t t;
struct tm *tm;
char buff[100];
Expand All @@ -53,13 +51,11 @@ stderr_log_with_level(const char *level_name, int level, const char *fmt,...)
{
time(&t);
tm = localtime(&t);

va_start(ap, fmt);

strftime(buff, 100, "[%Y-%m-%d %H:%M:%S]", tm);
snprintf(fmt1, len + 150, "%s [%s] %s", buff, level_name, fmt);
vfprintf(stderr, fmt1, ap);
fprintf(stderr, "%s [%s] ", buff, level_name);

va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);

fflush(stderr);
Expand Down
12 changes: 6 additions & 6 deletions repmgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ main(int argc, char **argv)
}

/* Prepare the repmgr schema variable */
snprintf(repmgr_schema, MAXLEN, "%s%s", DEFAULT_REPMGR_SCHEMA_PREFIX,
maxlen_snprintf(repmgr_schema, "%s%s", DEFAULT_REPMGR_SCHEMA_PREFIX,
options.cluster_name);

switch (action)
Expand Down Expand Up @@ -1698,7 +1698,7 @@ do_witness_create(void)
* default port for the witness is 5499, but user can provide a different
* one
*/
snprintf(buf, sizeof(buf), "%s/postgresql.conf", runtime_options.dest_dir);
xsnprintf(buf, sizeof(buf), "%s/postgresql.conf", runtime_options.dest_dir);
pg_conf = fopen(buf, "a");
if (pg_conf == NULL)
{
Expand All @@ -1708,18 +1708,18 @@ do_witness_create(void)
exit(ERR_BAD_CONFIG);
}

snprintf(buf, sizeof(buf), "\n#Configuration added by %s\n", progname);
xsnprintf(buf, sizeof(buf), "\n#Configuration added by %s\n", progname);
fputs(buf, pg_conf);

if (!runtime_options.localport[0])
strncpy(runtime_options.localport, "5499", MAXLEN);
snprintf(buf, sizeof(buf), "port = %s\n", runtime_options.localport);
xsnprintf(buf, sizeof(buf), "port = %s\n", runtime_options.localport);
fputs(buf, pg_conf);

snprintf(buf, sizeof(buf), "shared_preload_libraries = 'repmgr_funcs'\n");
xsnprintf(buf, sizeof(buf), "shared_preload_libraries = 'repmgr_funcs'\n");
fputs(buf, pg_conf);

snprintf(buf, sizeof(buf), "listen_addresses = '*'\n");
xsnprintf(buf, sizeof(buf), "listen_addresses = '*'\n");
fputs(buf, pg_conf);

fclose(pg_conf);
Expand Down
2 changes: 1 addition & 1 deletion repmgrd.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ main(int argc, char **argv)
}
}

snprintf(repmgr_schema, MAXLEN, "%s%s", DEFAULT_REPMGR_SCHEMA_PREFIX,
xsnprintf(repmgr_schema, MAXLEN, "%s%s", DEFAULT_REPMGR_SCHEMA_PREFIX,
local_options.cluster_name);

log_info(_("%s Connecting to database '%s'\n"), progname,
Expand Down

0 comments on commit 9eba986

Please sign in to comment.