Skip to content

Commit edc4345

Browse files
committed
Add more use of psprintf()
1 parent 10a82cd commit edc4345

File tree

15 files changed

+52
-119
lines changed

15 files changed

+52
-119
lines changed

contrib/dblink/dblink.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,10 +1563,7 @@ dblink_get_pkey(PG_FUNCTION_ARGS)
15631563
Datum result;
15641564

15651565
values = (char **) palloc(2 * sizeof(char *));
1566-
values[0] = (char *) palloc(12); /* sign, 10 digits, '\0' */
1567-
1568-
sprintf(values[0], "%d", call_cntr + 1);
1569-
1566+
values[0] = psprintf("%d", call_cntr + 1);
15701567
values[1] = results[call_cntr];
15711568

15721569
/* build the tuple */

contrib/hstore/hstore_io.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,11 +1114,7 @@ hstore_out(PG_FUNCTION_ARGS)
11141114
HEntry *entries = ARRPTR(in);
11151115

11161116
if (count == 0)
1117-
{
1118-
out = palloc(1);
1119-
*out = '\0';
1120-
PG_RETURN_CSTRING(out);
1121-
}
1117+
PG_RETURN_CSTRING(pstrdup(""));
11221118

11231119
buflen = 0;
11241120

contrib/pageinspect/btreefuncs.c

Lines changed: 24 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -220,31 +220,17 @@ bt_page_stats(PG_FUNCTION_ARGS)
220220
elog(ERROR, "return type must be a row type");
221221

222222
j = 0;
223-
values[j] = palloc(32);
224-
snprintf(values[j++], 32, "%d", stat.blkno);
225-
values[j] = palloc(32);
226-
snprintf(values[j++], 32, "%c", stat.type);
227-
values[j] = palloc(32);
228-
snprintf(values[j++], 32, "%d", stat.live_items);
229-
values[j] = palloc(32);
230-
snprintf(values[j++], 32, "%d", stat.dead_items);
231-
values[j] = palloc(32);
232-
snprintf(values[j++], 32, "%d", stat.avg_item_size);
233-
values[j] = palloc(32);
234-
snprintf(values[j++], 32, "%d", stat.page_size);
235-
values[j] = palloc(32);
236-
snprintf(values[j++], 32, "%d", stat.free_size);
237-
values[j] = palloc(32);
238-
snprintf(values[j++], 32, "%d", stat.btpo_prev);
239-
values[j] = palloc(32);
240-
snprintf(values[j++], 32, "%d", stat.btpo_next);
241-
values[j] = palloc(32);
242-
if (stat.type == 'd')
243-
snprintf(values[j++], 32, "%d", stat.btpo.xact);
244-
else
245-
snprintf(values[j++], 32, "%d", stat.btpo.level);
246-
values[j] = palloc(32);
247-
snprintf(values[j++], 32, "%d", stat.btpo_flags);
223+
values[j++] = psprintf("%d", stat.blkno);
224+
values[j++] = psprintf("%c", stat.type);
225+
values[j++] = psprintf("%d", stat.live_items);
226+
values[j++] = psprintf("%d", stat.dead_items);
227+
values[j++] = psprintf("%d", stat.avg_item_size);
228+
values[j++] = psprintf("%d", stat.page_size);
229+
values[j++] = psprintf("%d", stat.free_size);
230+
values[j++] = psprintf("%d", stat.btpo_prev);
231+
values[j++] = psprintf("%d", stat.btpo_next);
232+
values[j++] = psprintf("%d", (stat.type == 'd') ? stat.btpo.xact : stat.btpo.level);
233+
values[j++] = psprintf("%d", stat.btpo_flags);
248234

249235
tuple = BuildTupleFromCStrings(TupleDescGetAttInMetadata(tupleDesc),
250236
values);
@@ -380,18 +366,13 @@ bt_page_items(PG_FUNCTION_ARGS)
380366
itup = (IndexTuple) PageGetItem(uargs->page, id);
381367

382368
j = 0;
383-
values[j] = palloc(32);
384-
snprintf(values[j++], 32, "%d", uargs->offset);
385-
values[j] = palloc(32);
386-
snprintf(values[j++], 32, "(%u,%u)",
387-
BlockIdGetBlockNumber(&(itup->t_tid.ip_blkid)),
388-
itup->t_tid.ip_posid);
389-
values[j] = palloc(32);
390-
snprintf(values[j++], 32, "%d", (int) IndexTupleSize(itup));
391-
values[j] = palloc(32);
392-
snprintf(values[j++], 32, "%c", IndexTupleHasNulls(itup) ? 't' : 'f');
393-
values[j] = palloc(32);
394-
snprintf(values[j++], 32, "%c", IndexTupleHasVarwidths(itup) ? 't' : 'f');
369+
values[j++] = psprintf("%d", uargs->offset);
370+
values[j++] = psprintf("(%u,%u)",
371+
BlockIdGetBlockNumber(&(itup->t_tid.ip_blkid)),
372+
itup->t_tid.ip_posid);
373+
values[j++] = psprintf("%d", (int) IndexTupleSize(itup));
374+
values[j++] = psprintf("%c", IndexTupleHasNulls(itup) ? 't' : 'f');
375+
values[j++] = psprintf("%c", IndexTupleHasVarwidths(itup) ? 't' : 'f');
395376

396377
ptr = (char *) itup + IndexInfoFindDataOffset(itup->t_info);
397378
dlen = IndexTupleSize(itup) - IndexInfoFindDataOffset(itup->t_info);
@@ -477,18 +458,12 @@ bt_metap(PG_FUNCTION_ARGS)
477458
elog(ERROR, "return type must be a row type");
478459

479460
j = 0;
480-
values[j] = palloc(32);
481-
snprintf(values[j++], 32, "%d", metad->btm_magic);
482-
values[j] = palloc(32);
483-
snprintf(values[j++], 32, "%d", metad->btm_version);
484-
values[j] = palloc(32);
485-
snprintf(values[j++], 32, "%d", metad->btm_root);
486-
values[j] = palloc(32);
487-
snprintf(values[j++], 32, "%d", metad->btm_level);
488-
values[j] = palloc(32);
489-
snprintf(values[j++], 32, "%d", metad->btm_fastroot);
490-
values[j] = palloc(32);
491-
snprintf(values[j++], 32, "%d", metad->btm_fastlevel);
461+
values[j++] = psprintf("%d", metad->btm_magic);
462+
values[j++] = psprintf("%d", metad->btm_version);
463+
values[j++] = psprintf("%d", metad->btm_root);
464+
values[j++] = psprintf("%d", metad->btm_level);
465+
values[j++] = psprintf("%d", metad->btm_fastroot);
466+
values[j++] = psprintf("%d", metad->btm_fastlevel);
492467

493468
tuple = BuildTupleFromCStrings(TupleDescGetAttInMetadata(tupleDesc),
494469
values);

contrib/pgstattuple/pgstatindex.c

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -271,39 +271,29 @@ pgstatindex_impl(Relation rel, FunctionCallInfo fcinfo)
271271
elog(ERROR, "return type must be a row type");
272272

273273
j = 0;
274-
values[j] = palloc(32);
275-
snprintf(values[j++], 32, "%d", indexStat.version);
276-
values[j] = palloc(32);
277-
snprintf(values[j++], 32, "%d", indexStat.level);
278-
values[j] = palloc(32);
279-
snprintf(values[j++], 32, INT64_FORMAT,
274+
values[j++] = psprintf("%d", indexStat.version);
275+
values[j++] = psprintf("%d", indexStat.level);
276+
values[j++] = psprintf(INT64_FORMAT,
280277
(indexStat.root_pages +
281278
indexStat.leaf_pages +
282279
indexStat.internal_pages +
283280
indexStat.deleted_pages +
284281
indexStat.empty_pages) * BLCKSZ);
285-
values[j] = palloc(32);
286-
snprintf(values[j++], 32, "%u", indexStat.root_blkno);
287-
values[j] = palloc(32);
288-
snprintf(values[j++], 32, INT64_FORMAT, indexStat.internal_pages);
289-
values[j] = palloc(32);
290-
snprintf(values[j++], 32, INT64_FORMAT, indexStat.leaf_pages);
291-
values[j] = palloc(32);
292-
snprintf(values[j++], 32, INT64_FORMAT, indexStat.empty_pages);
293-
values[j] = palloc(32);
294-
snprintf(values[j++], 32, INT64_FORMAT, indexStat.deleted_pages);
295-
values[j] = palloc(32);
282+
values[j++] = psprintf("%u", indexStat.root_blkno);
283+
values[j++] = psprintf(INT64_FORMAT, indexStat.internal_pages);
284+
values[j++] = psprintf(INT64_FORMAT, indexStat.leaf_pages);
285+
values[j++] = psprintf(INT64_FORMAT, indexStat.empty_pages);
286+
values[j++] = psprintf(INT64_FORMAT, indexStat.deleted_pages);
296287
if (indexStat.max_avail > 0)
297-
snprintf(values[j++], 32, "%.2f",
288+
values[j++] = psprintf("%.2f",
298289
100.0 - (double) indexStat.free_space / (double) indexStat.max_avail * 100.0);
299290
else
300-
snprintf(values[j++], 32, "NaN");
301-
values[j] = palloc(32);
291+
values[j++] = pstrdup("NaN");
302292
if (indexStat.leaf_pages > 0)
303-
snprintf(values[j++], 32, "%.2f",
293+
values[j++] = psprintf("%.2f",
304294
(double) indexStat.fragments / (double) indexStat.leaf_pages * 100.0);
305295
else
306-
snprintf(values[j++], 32, "NaN");
296+
values[j++] = pstrdup("NaN");
307297

308298
tuple = BuildTupleFromCStrings(TupleDescGetAttInMetadata(tupleDesc),
309299
values);

doc/src/sgml/xtypes.sgml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ complex_out(PG_FUNCTION_ARGS)
108108
Complex *complex = (Complex *) PG_GETARG_POINTER(0);
109109
char *result;
110110

111-
result = (char *) palloc(100);
112-
snprintf(result, 100, "(%g,%g)", complex->x, complex->y);
111+
result = psprintf("(%g,%g)", complex->x, complex->y);
113112
PG_RETURN_CSTRING(result);
114113
}
115114
]]>

src/backend/access/transam/multixact.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2757,8 +2757,7 @@ pg_get_multixact_members(PG_FUNCTION_ARGS)
27572757
HeapTuple tuple;
27582758
char *values[2];
27592759

2760-
values[0] = palloc(32);
2761-
sprintf(values[0], "%u", multi->members[multi->iter].xid);
2760+
values[0] = psprintf("%u", multi->members[multi->iter].xid);
27622761
values[1] = mxstatus_to_string(multi->members[multi->iter].status);
27632762

27642763
tuple = BuildTupleFromCStrings(funccxt->attinmeta, values);

src/backend/commands/define.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,7 @@ defGetString(DefElem *def)
5656
switch (nodeTag(def->arg))
5757
{
5858
case T_Integer:
59-
{
60-
char *str = palloc(32);
61-
62-
snprintf(str, 32, "%ld", (long) intVal(def->arg));
63-
return str;
64-
}
59+
return psprintf("%ld", (long) intVal(def->arg));
6560
case T_Float:
6661

6762
/*

src/backend/optimizer/plan/subselect.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -778,10 +778,7 @@ build_subplan(PlannerInfo *root, Plan *plan, PlannerInfo *subroot,
778778
sprintf(splan->plan_name + offset, ")");
779779
}
780780
else
781-
{
782-
splan->plan_name = palloc(32);
783-
sprintf(splan->plan_name, "SubPlan %d", splan->plan_id);
784-
}
781+
splan->plan_name = psprintf("SubPlan %d", splan->plan_id);
785782

786783
/* Lastly, fill in the cost estimates for use later */
787784
cost_subplan(root, splan, plan);
@@ -2600,8 +2597,7 @@ SS_make_initplan_from_plan(PlannerInfo *root, Plan *plan,
26002597
node->setParam = list_make1_int(prm->paramid);
26012598

26022599
/* Label the subplan for EXPLAIN purposes */
2603-
node->plan_name = palloc(64);
2604-
sprintf(node->plan_name, "InitPlan %d (returns $%d)",
2600+
node->plan_name = psprintf("InitPlan %d (returns $%d)",
26052601
node->plan_id, prm->paramid);
26062602

26072603
return prm;

src/backend/parser/parse_type.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,7 @@ typenameTypeMod(ParseState *pstate, const TypeName *typeName, Type typ)
313313

314314
if (IsA(&ac->val, Integer))
315315
{
316-
cstr = (char *) palloc(32);
317-
snprintf(cstr, 32, "%ld", (long) ac->val.val.ival);
316+
cstr = psprintf("%ld", (long) ac->val.val.ival);
318317
}
319318
else if (IsA(&ac->val, Float) ||
320319
IsA(&ac->val, String))

src/backend/storage/smgr/md.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,9 +1649,7 @@ _mdfd_segpath(SMgrRelation reln, ForkNumber forknum, BlockNumber segno)
16491649

16501650
if (segno > 0)
16511651
{
1652-
/* be sure we have enough space for the '.segno' */
1653-
fullpath = (char *) palloc(strlen(path) + 12);
1654-
sprintf(fullpath, "%s.%u", path, segno);
1652+
fullpath = psprintf("%s.%u", path, segno);
16551653
pfree(path);
16561654
}
16571655
else

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