1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-31 22:04:40 +03:00

Add use of asprintf()

Add asprintf(), pg_asprintf(), and psprintf() to simplify string
allocation and composition.  Replacement implementations taken from
NetBSD.

Reviewed-by: Álvaro Herrera <alvherre@2ndquadrant.com>
Reviewed-by: Asif Naeem <anaeem.it@gmail.com>
This commit is contained in:
Peter Eisentraut
2013-10-13 00:09:18 -04:00
parent a53dee43fe
commit 5b6d08cd29
47 changed files with 253 additions and 363 deletions

View File

@ -376,8 +376,7 @@ pg_logdir_ls(PG_FUNCTION_ARGS)
/* Seems the timestamp is OK; prepare and return tuple */
values[0] = timestampbuf;
values[1] = palloc(strlen(fctx->location) + strlen(de->d_name) + 2);
sprintf(values[1], "%s/%s", fctx->location, de->d_name);
values[1] = psprintf("%s/%s", fctx->location, de->d_name);
tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);

View File

@ -508,8 +508,7 @@ sql_exec_searchtables(PGconn *conn, struct options * opts)
free(comma_filenodes);
/* now build the query */
todo = (char *) pg_malloc(650 + strlen(qualifiers));
snprintf(todo, 650 + strlen(qualifiers),
pg_asprintf(&todo,
"SELECT pg_catalog.pg_relation_filenode(c.oid) as \"Filenode\", relname as \"Table Name\" %s\n"
"FROM pg_catalog.pg_class c \n"
" LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \n"

View File

@ -455,18 +455,13 @@ create_script_for_cluster_analyze(char **analyze_script_file_name)
FILE *script = NULL;
char *user_specification = "";
if (os_info.user_specified)
{
user_specification = pg_malloc(strlen(os_info.user) + 7);
sprintf(user_specification, "-U \"%s\" ", os_info.user);
}
*analyze_script_file_name = pg_malloc(MAXPGPATH);
prep_status("Creating script to analyze new cluster");
snprintf(*analyze_script_file_name, MAXPGPATH, "analyze_new_cluster.%s",
SCRIPT_EXT);
if (os_info.user_specified)
pg_asprintf(&user_specification, "-U \"%s\" ", os_info.user);
pg_asprintf(analyze_script_file_name, "analyze_new_cluster.%s",
SCRIPT_EXT);
if ((script = fopen_priv(*analyze_script_file_name, "w")) == NULL)
pg_fatal("Could not open file \"%s\": %s\n",
@ -597,10 +592,8 @@ create_script_for_old_cluster_deletion(char **deletion_script_file_name)
int tblnum;
char old_cluster_pgdata[MAXPGPATH];
*deletion_script_file_name = pg_malloc(MAXPGPATH);
snprintf(*deletion_script_file_name, MAXPGPATH, "delete_old_cluster.%s",
SCRIPT_EXT);
pg_asprintf(deletion_script_file_name, "delete_old_cluster.%s",
SCRIPT_EXT);
/*
* Some users (oddly) create tablespaces inside the cluster data

View File

@ -84,12 +84,9 @@ set_tablespace_directory_suffix(ClusterInfo *cluster)
else
{
/* This cluster has a version-specific subdirectory */
cluster->tablespace_suffix = pg_malloc(4 +
strlen(cluster->major_version_str) +
10 /* OIDCHARS */ + 1);
/* The leading slash is needed to start a new directory. */
sprintf(cluster->tablespace_suffix, "/PG_%s_%d", cluster->major_version_str,
cluster->controldata.cat_ver);
pg_asprintf(&cluster->tablespace_suffix, "/PG_%s_%d",
cluster->major_version_str, cluster->controldata.cat_ver);
}
}

View File

@ -276,10 +276,9 @@ pg_putenv(const char *var, const char *val)
if (val)
{
#ifndef WIN32
char *envstr = (char *) pg_malloc(strlen(var) +
strlen(val) + 2);
char *envstr;
sprintf(envstr, "%s=%s", var, val);
pg_asprintf(&envstr, "%s=%s", var, val);
putenv(envstr);
/*

View File

@ -675,9 +675,9 @@ old_8_3_create_sequence_script(ClusterInfo *cluster)
int dbnum;
FILE *script = NULL;
bool found = false;
char *output_path = pg_malloc(MAXPGPATH);
char *output_path;
snprintf(output_path, MAXPGPATH, "adjust_sequences.sql");
output_path = pg_strdup("adjust_sequences.sql");
prep_status("Creating script to adjust sequences");

View File

@ -634,8 +634,7 @@ find_plan(char *ident, EPlan **eplan, int *nplans)
(*nplans) = i = 0;
}
newp->ident = (char *) malloc(strlen(ident) + 1);
strcpy(newp->ident, ident);
newp->ident = strdup(ident);
newp->nplans = 0;
newp->splan = NULL;
(*nplans)++;

View File

@ -540,8 +540,7 @@ find_plan(char *ident, EPlan **eplan, int *nplans)
(*nplans) = i = 0;
}
newp->ident = (char *) malloc(strlen(ident) + 1);
strcpy(newp->ident, ident);
newp->ident = strdup(ident);
newp->splan = NULL;
(*nplans)++;