Skip to content

Commit 5b6d08c

Browse files
committed
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>
1 parent a53dee4 commit 5b6d08c

File tree

47 files changed

+253
-363
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+253
-363
lines changed

configure

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21502,7 +21502,8 @@ fi
2150221502

2150321503

2150421504

21505-
for ac_func in crypt fls getopt getrusage inet_aton random rint srandom strerror strlcat strlcpy
21505+
21506+
for ac_func in asprintf crypt fls getopt getrusage inet_aton random rint srandom strerror strlcat strlcpy
2150621507
do
2150721508
as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
2150821509
{ $as_echo "$as_me:$LINENO: checking for $ac_func" >&5

configure.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1346,7 +1346,7 @@ else
13461346
AC_CHECK_FUNCS([fpclass fp_class fp_class_d class], [break])
13471347
fi
13481348

1349-
AC_REPLACE_FUNCS([crypt fls getopt getrusage inet_aton random rint srandom strerror strlcat strlcpy])
1349+
AC_REPLACE_FUNCS([asprintf crypt fls getopt getrusage inet_aton random rint srandom strerror strlcat strlcpy])
13501350

13511351
case $host_os in
13521352

contrib/adminpack/adminpack.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,7 @@ pg_logdir_ls(PG_FUNCTION_ARGS)
376376
/* Seems the timestamp is OK; prepare and return tuple */
377377

378378
values[0] = timestampbuf;
379-
values[1] = palloc(strlen(fctx->location) + strlen(de->d_name) + 2);
380-
sprintf(values[1], "%s/%s", fctx->location, de->d_name);
379+
values[1] = psprintf("%s/%s", fctx->location, de->d_name);
381380

382381
tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
383382

contrib/oid2name/oid2name.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,8 +508,7 @@ sql_exec_searchtables(PGconn *conn, struct options * opts)
508508
free(comma_filenodes);
509509

510510
/* now build the query */
511-
todo = (char *) pg_malloc(650 + strlen(qualifiers));
512-
snprintf(todo, 650 + strlen(qualifiers),
511+
pg_asprintf(&todo,
513512
"SELECT pg_catalog.pg_relation_filenode(c.oid) as \"Filenode\", relname as \"Table Name\" %s\n"
514513
"FROM pg_catalog.pg_class c \n"
515514
" LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \n"

contrib/pg_upgrade/check.c

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -455,18 +455,13 @@ create_script_for_cluster_analyze(char **analyze_script_file_name)
455455
FILE *script = NULL;
456456
char *user_specification = "";
457457

458-
if (os_info.user_specified)
459-
{
460-
user_specification = pg_malloc(strlen(os_info.user) + 7);
461-
sprintf(user_specification, "-U \"%s\" ", os_info.user);
462-
}
463-
464-
*analyze_script_file_name = pg_malloc(MAXPGPATH);
465-
466458
prep_status("Creating script to analyze new cluster");
467459

468-
snprintf(*analyze_script_file_name, MAXPGPATH, "analyze_new_cluster.%s",
469-
SCRIPT_EXT);
460+
if (os_info.user_specified)
461+
pg_asprintf(&user_specification, "-U \"%s\" ", os_info.user);
462+
463+
pg_asprintf(analyze_script_file_name, "analyze_new_cluster.%s",
464+
SCRIPT_EXT);
470465

471466
if ((script = fopen_priv(*analyze_script_file_name, "w")) == NULL)
472467
pg_fatal("Could not open file \"%s\": %s\n",
@@ -597,10 +592,8 @@ create_script_for_old_cluster_deletion(char **deletion_script_file_name)
597592
int tblnum;
598593
char old_cluster_pgdata[MAXPGPATH];
599594

600-
*deletion_script_file_name = pg_malloc(MAXPGPATH);
601-
602-
snprintf(*deletion_script_file_name, MAXPGPATH, "delete_old_cluster.%s",
603-
SCRIPT_EXT);
595+
pg_asprintf(deletion_script_file_name, "delete_old_cluster.%s",
596+
SCRIPT_EXT);
604597

605598
/*
606599
* Some users (oddly) create tablespaces inside the cluster data

contrib/pg_upgrade/tablespace.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,9 @@ set_tablespace_directory_suffix(ClusterInfo *cluster)
8484
else
8585
{
8686
/* This cluster has a version-specific subdirectory */
87-
cluster->tablespace_suffix = pg_malloc(4 +
88-
strlen(cluster->major_version_str) +
89-
10 /* OIDCHARS */ + 1);
9087

9188
/* The leading slash is needed to start a new directory. */
92-
sprintf(cluster->tablespace_suffix, "/PG_%s_%d", cluster->major_version_str,
93-
cluster->controldata.cat_ver);
89+
pg_asprintf(&cluster->tablespace_suffix, "/PG_%s_%d",
90+
cluster->major_version_str, cluster->controldata.cat_ver);
9491
}
9592
}

contrib/pg_upgrade/util.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,9 @@ pg_putenv(const char *var, const char *val)
276276
if (val)
277277
{
278278
#ifndef WIN32
279-
char *envstr = (char *) pg_malloc(strlen(var) +
280-
strlen(val) + 2);
279+
char *envstr;
281280

282-
sprintf(envstr, "%s=%s", var, val);
281+
pg_asprintf(&envstr, "%s=%s", var, val);
283282
putenv(envstr);
284283

285284
/*

contrib/pg_upgrade/version_old_8_3.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,9 +675,9 @@ old_8_3_create_sequence_script(ClusterInfo *cluster)
675675
int dbnum;
676676
FILE *script = NULL;
677677
bool found = false;
678-
char *output_path = pg_malloc(MAXPGPATH);
678+
char *output_path;
679679

680-
snprintf(output_path, MAXPGPATH, "adjust_sequences.sql");
680+
output_path = pg_strdup("adjust_sequences.sql");
681681

682682
prep_status("Creating script to adjust sequences");
683683

contrib/spi/refint.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -634,8 +634,7 @@ find_plan(char *ident, EPlan **eplan, int *nplans)
634634
(*nplans) = i = 0;
635635
}
636636

637-
newp->ident = (char *) malloc(strlen(ident) + 1);
638-
strcpy(newp->ident, ident);
637+
newp->ident = strdup(ident);
639638
newp->nplans = 0;
640639
newp->splan = NULL;
641640
(*nplans)++;

contrib/spi/timetravel.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,8 +540,7 @@ find_plan(char *ident, EPlan **eplan, int *nplans)
540540
(*nplans) = i = 0;
541541
}
542542

543-
newp->ident = (char *) malloc(strlen(ident) + 1);
544-
strcpy(newp->ident, ident);
543+
newp->ident = strdup(ident);
545544
newp->splan = NULL;
546545
(*nplans)++;
547546

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy