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

src/backend/bootstrap/bootstrap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,9 @@ AuxiliaryProcessMain(int argc, char *argv[])
249249
case 'd':
250250
{
251251
/* Turn on debugging for the bootstrap process. */
252-
char *debugstr = palloc(strlen("debug") + strlen(optarg) + 1);
252+
char *debugstr;
253253

254-
sprintf(debugstr, "debug%s", optarg);
254+
debugstr = psprintf("debug%s", optarg);
255255
SetConfigOption("log_min_messages", debugstr,
256256
PGC_POSTMASTER, PGC_S_ARGV);
257257
SetConfigOption("client_min_messages", debugstr,

src/backend/catalog/catalog.c

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,35 +75,23 @@ forkname_to_number(char *forkName)
7575
char *
7676
GetDatabasePath(Oid dbNode, Oid spcNode)
7777
{
78-
int pathlen;
79-
char *path;
80-
8178
if (spcNode == GLOBALTABLESPACE_OID)
8279
{
8380
/* Shared system relations live in {datadir}/global */
8481
Assert(dbNode == 0);
85-
pathlen = 6 + 1;
86-
path = (char *) palloc(pathlen);
87-
snprintf(path, pathlen, "global");
82+
return pstrdup("global");
8883
}
8984
else if (spcNode == DEFAULTTABLESPACE_OID)
9085
{
9186
/* The default tablespace is {datadir}/base */
92-
pathlen = 5 + OIDCHARS + 1;
93-
path = (char *) palloc(pathlen);
94-
snprintf(path, pathlen, "base/%u",
95-
dbNode);
87+
return psprintf("base/%u", dbNode);
9688
}
9789
else
9890
{
9991
/* All other tablespaces are accessed via symlinks */
100-
pathlen = 9 + 1 + OIDCHARS + 1 + strlen(TABLESPACE_VERSION_DIRECTORY) +
101-
1 + OIDCHARS + 1;
102-
path = (char *) palloc(pathlen);
103-
snprintf(path, pathlen, "pg_tblspc/%u/%s/%u",
104-
spcNode, TABLESPACE_VERSION_DIRECTORY, dbNode);
92+
return psprintf("pg_tblspc/%u/%s/%u",
93+
spcNode, TABLESPACE_VERSION_DIRECTORY, dbNode);
10594
}
106-
return path;
10795
}
10896

10997

src/backend/commands/tablespace.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -541,12 +541,11 @@ DropTableSpace(DropTableSpaceStmt *stmt)
541541
static void
542542
create_tablespace_directories(const char *location, const Oid tablespaceoid)
543543
{
544-
char *linkloc = palloc(OIDCHARS + OIDCHARS + 1);
545-
char *location_with_version_dir = palloc(strlen(location) + 1 +
546-
strlen(TABLESPACE_VERSION_DIRECTORY) + 1);
544+
char *linkloc;
545+
char *location_with_version_dir;
547546

548-
sprintf(linkloc, "pg_tblspc/%u", tablespaceoid);
549-
sprintf(location_with_version_dir, "%s/%s", location,
547+
linkloc = psprintf("pg_tblspc/%u", tablespaceoid);
548+
location_with_version_dir = psprintf("%s/%s", location,
550549
TABLESPACE_VERSION_DIRECTORY);
551550

552551
/*
@@ -652,9 +651,7 @@ destroy_tablespace_directories(Oid tablespaceoid, bool redo)
652651
char *subfile;
653652
struct stat st;
654653

655-
linkloc_with_version_dir = palloc(9 + 1 + OIDCHARS + 1 +
656-
strlen(TABLESPACE_VERSION_DIRECTORY));
657-
sprintf(linkloc_with_version_dir, "pg_tblspc/%u/%s", tablespaceoid,
654+
linkloc_with_version_dir = psprintf("pg_tblspc/%u/%s", tablespaceoid,
658655
TABLESPACE_VERSION_DIRECTORY);
659656

660657
/*
@@ -711,8 +708,7 @@ destroy_tablespace_directories(Oid tablespaceoid, bool redo)
711708
strcmp(de->d_name, "..") == 0)
712709
continue;
713710

714-
subfile = palloc(strlen(linkloc_with_version_dir) + 1 + strlen(de->d_name) + 1);
715-
sprintf(subfile, "%s/%s", linkloc_with_version_dir, de->d_name);
711+
subfile = psprintf("%s/%s", linkloc_with_version_dir, de->d_name);
716712

717713
/* This check is just to deliver a friendlier error message */
718714
if (!redo && !directory_is_empty(subfile))

src/backend/libpq/auth.c

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,17 +1015,15 @@ pg_GSS_recvauth(Port *port)
10151015
*/
10161016
if (getenv("KRB5_KTNAME") == NULL)
10171017
{
1018-
size_t kt_len = strlen(pg_krb_server_keyfile) + 14;
1019-
char *kt_path = malloc(kt_len);
1018+
char *kt_path;
10201019

1021-
if (!kt_path)
1020+
if (asprintf(&kt_path, "KRB5_KTNAME=%s", pg_krb_server_keyfile) < 0)
10221021
{
10231022
ereport(LOG,
10241023
(errcode(ERRCODE_OUT_OF_MEMORY),
10251024
errmsg("out of memory")));
10261025
return STATUS_ERROR;
10271026
}
1028-
snprintf(kt_path, kt_len, "KRB5_KTNAME=%s", pg_krb_server_keyfile);
10291027
putenv(kt_path);
10301028
}
10311029
}
@@ -1488,8 +1486,7 @@ pg_SSPI_recvauth(Port *port)
14881486
char *namebuf;
14891487
int retval;
14901488

1491-
namebuf = palloc(strlen(accountname) + strlen(domainname) + 2);
1492-
sprintf(namebuf, "%s@%s", accountname, domainname);
1489+
namebuf = psprintf("%s@%s", accountname, domainname);
14931490
retval = check_usermap(port->hba->usermap, port->user_name, namebuf, true);
14941491
pfree(namebuf);
14951492
return retval;
@@ -2209,8 +2206,7 @@ CheckLDAPAuth(Port *port)
22092206
attributes[0] = port->hba->ldapsearchattribute ? port->hba->ldapsearchattribute : "uid";
22102207
attributes[1] = NULL;
22112208

2212-
filter = palloc(strlen(attributes[0]) + strlen(port->user_name) + 4);
2213-
sprintf(filter, "(%s=%s)",
2209+
filter = psprintf("(%s=%s)",
22142210
attributes[0],
22152211
port->user_name);
22162212

@@ -2299,17 +2295,10 @@ CheckLDAPAuth(Port *port)
22992295
}
23002296
}
23012297
else
2302-
{
2303-
fulluser = palloc((port->hba->ldapprefix ? strlen(port->hba->ldapprefix) : 0) +
2304-
strlen(port->user_name) +
2305-
(port->hba->ldapsuffix ? strlen(port->hba->ldapsuffix) : 0) +
2306-
1);
2307-
2308-
sprintf(fulluser, "%s%s%s",
2298+
fulluser = psprintf("%s%s%s",
23092299
port->hba->ldapprefix ? port->hba->ldapprefix : "",
23102300
port->user_name,
23112301
port->hba->ldapsuffix ? port->hba->ldapsuffix : "");
2312-
}
23132302

23142303
r = ldap_simple_bind_s(ldap, fulluser, passwd);
23152304
ldap_unbind(ldap);

src/backend/optimizer/plan/subselect.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,8 +1119,7 @@ SS_process_ctes(PlannerInfo *root)
11191119
root->cte_plan_ids = lappend_int(root->cte_plan_ids, splan->plan_id);
11201120

11211121
/* Label the subplan for EXPLAIN purposes */
1122-
splan->plan_name = palloc(4 + strlen(cte->ctename) + 1);
1123-
sprintf(splan->plan_name, "CTE %s", cte->ctename);
1122+
splan->plan_name = psprintf("CTE %s", cte->ctename);
11241123

11251124
/* Lastly, fill in the cost estimates for use later */
11261125
cost_subplan(root, splan, plan);

src/backend/parser/gram.y

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,10 +1450,7 @@ set_rest_more: /* Generic SET syntaxes: */
14501450

14511451
var_name: ColId { $$ = $1; }
14521452
| var_name '.' ColId
1453-
{
1454-
$$ = palloc(strlen($1) + strlen($3) + 2);
1455-
sprintf($$, "%s.%s", $1, $3);
1456-
}
1453+
{ $$ = psprintf("%s.%s", $1, $3); }
14571454
;
14581455

14591456
var_list: var_value { $$ = list_make1($1); }
@@ -10327,15 +10324,7 @@ ConstCharacter: CharacterWithLength
1032710324
CharacterWithLength: character '(' Iconst ')' opt_charset
1032810325
{
1032910326
if (($5 != NULL) && (strcmp($5, "sql_text") != 0))
10330-
{
10331-
char *type;
10332-
10333-
type = palloc(strlen($1) + 1 + strlen($5) + 1);
10334-
strcpy(type, $1);
10335-
strcat(type, "_");
10336-
strcat(type, $5);
10337-
$1 = type;
10338-
}
10327+
$1 = psprintf("%s_%s", $1, $5);
1033910328

1034010329
$$ = SystemTypeName($1);
1034110330
$$->typmods = list_make1(makeIntConst($3, @3));
@@ -10346,15 +10335,7 @@ CharacterWithLength: character '(' Iconst ')' opt_charset
1034610335
CharacterWithoutLength: character opt_charset
1034710336
{
1034810337
if (($2 != NULL) && (strcmp($2, "sql_text") != 0))
10349-
{
10350-
char *type;
10351-
10352-
type = palloc(strlen($1) + 1 + strlen($2) + 1);
10353-
strcpy(type, $1);
10354-
strcat(type, "_");
10355-
strcat(type, $2);
10356-
$1 = type;
10357-
}
10338+
$1 = psprintf("%s_%s", $1, $2);
1035810339

1035910340
$$ = SystemTypeName($1);
1036010341

@@ -13339,13 +13320,7 @@ doNegateFloat(Value *v)
1333913320
if (*oldval == '-')
1334013321
v->val.str = oldval+1; /* just strip the '-' */
1334113322
else
13342-
{
13343-
char *newval = (char *) palloc(strlen(oldval) + 2);
13344-
13345-
*newval = '-';
13346-
strcpy(newval+1, oldval);
13347-
v->val.str = newval;
13348-
}
13323+
v->val.str = psprintf("-%s", oldval);
1334913324
}
1335013325

1335113326
static Node *

src/backend/postmaster/postmaster.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1969,12 +1969,7 @@ ProcessStartupPacket(Port *port, bool SSLdone)
19691969
else
19701970
{
19711971
/* Append '@' and dbname */
1972-
char *db_user;
1973-
1974-
db_user = palloc(strlen(port->user_name) +
1975-
strlen(port->database_name) + 2);
1976-
sprintf(db_user, "%s@%s", port->user_name, port->database_name);
1977-
port->user_name = db_user;
1972+
port->user_name = psprintf("%s@%s", port->user_name, port->database_name);
19781973
}
19791974
}
19801975

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