avoid usage of snprintf()

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!
This commit is contained in:
Christian Kruse
2014-03-06 18:11:04 +01:00
parent 164cf9d08f
commit 9eba986833
3 changed files with 11 additions and 15 deletions

12
log.c
View File

@@ -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];
@@ -53,13 +51,11 @@ stderr_log_with_level(const char *level_name, int level, const char *fmt,...)
{
time(&t);
tm = localtime(&t);
strftime(buff, 100, "[%Y-%m-%d %H:%M:%S]", tm);
fprintf(stderr, "%s [%s] ", buff, level_name);
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);
vfprintf(stderr, fmt, ap);
va_end(ap);
fflush(stderr);

View File

@@ -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)
@@ -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)
{
@@ -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);

View File

@@ -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,