Skip to content

Commit 8b26769

Browse files
committed
Fix an assortment of improper usages of string functions
In a similar effort to f736e18 and 110d817, fixup various usages of string functions where a more appropriate function is available and more fit for purpose. These changes include: 1. Use cstring_to_text_with_len() instead of cstring_to_text() when working with a StringInfoData and the length can easily be obtained. 2. Use appendStringInfoString() instead of appendStringInfo() when no formatting is required. 3. Use pstrdup(...) instead of psprintf("%s", ...) 4. Use pstrdup(...) instead of psprintf(...) (with no formatting) 5. Use appendPQExpBufferChar() instead of appendPQExpBufferStr() when the length of the string being appended is 1. 6. appendStringInfoChar() instead of appendStringInfo() when no formatting is required and string is 1 char long. 7. Use appendPQExpBufferStr(b, .) instead of appendPQExpBuffer(b, "%s", .) 8. Don't use pstrdup when it's fine to just point to the string constant. I (David) did find other cases of postgres#8 but opted to use #4 instead as I wasn't certain enough that applying postgres#8 was ok (e.g in hba.c) Author: Ranier Vilela, David Rowley Discussion: https://postgr.es/m/CAApHDvo2j2+RJBGhNtUz6BxabWWh2Jx16wMUMWKUjv70Ver1vg@mail.gmail.com
1 parent 6bcda4a commit 8b26769

File tree

18 files changed

+277
-284
lines changed

18 files changed

+277
-284
lines changed

Diff for: contrib/hstore/hstore_io.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1325,7 +1325,7 @@ hstore_to_json_loose(PG_FUNCTION_ARGS)
13251325
}
13261326
appendStringInfoChar(&dst, '}');
13271327

1328-
PG_RETURN_TEXT_P(cstring_to_text(dst.data));
1328+
PG_RETURN_TEXT_P(cstring_to_text_with_len(dst.data, dst.len));
13291329
}
13301330

13311331
PG_FUNCTION_INFO_V1(hstore_to_json);
@@ -1370,7 +1370,7 @@ hstore_to_json(PG_FUNCTION_ARGS)
13701370
}
13711371
appendStringInfoChar(&dst, '}');
13721372

1373-
PG_RETURN_TEXT_P(cstring_to_text(dst.data));
1373+
PG_RETURN_TEXT_P(cstring_to_text_with_len(dst.data, dst.len));
13741374
}
13751375

13761376
PG_FUNCTION_INFO_V1(hstore_to_jsonb);

Diff for: contrib/sepgsql/schema.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ sepgsql_schema_post_create(Oid namespaceId)
8484
* check db_schema:{create}
8585
*/
8686
initStringInfo(&audit_name);
87-
appendStringInfo(&audit_name, "%s", quote_identifier(nsp_name));
87+
appendStringInfoString(&audit_name, quote_identifier(nsp_name));
8888
sepgsql_avc_check_perms_label(ncontext,
8989
SEPG_CLASS_DB_SCHEMA,
9090
SEPG_DB_SCHEMA__CREATE,

Diff for: src/backend/access/brin/brin_minmax_multi.c

+2-8
Original file line numberDiff line numberDiff line change
@@ -3063,7 +3063,7 @@ brin_minmax_multi_summary_out(PG_FUNCTION_ARGS)
30633063

30643064
appendStringInfo(&str, "%s ... %s", a, b);
30653065

3066-
c = cstring_to_text(str.data);
3066+
c = cstring_to_text_with_len(str.data, str.len);
30673067

30683068
astate_values = accumArrayResult(astate_values,
30693069
PointerGetDatum(c),
@@ -3095,15 +3095,9 @@ brin_minmax_multi_summary_out(PG_FUNCTION_ARGS)
30953095
{
30963096
Datum a;
30973097
text *b;
3098-
StringInfoData str;
3099-
3100-
initStringInfo(&str);
31013098

31023099
a = FunctionCall1(&fmgrinfo, ranges_deserialized->values[idx++]);
3103-
3104-
appendStringInfoString(&str, DatumGetCString(a));
3105-
3106-
b = cstring_to_text(str.data);
3100+
b = cstring_to_text(DatumGetCString(a));
31073101

31083102
astate_values = accumArrayResult(astate_values,
31093103
PointerGetDatum(b),

Diff for: src/backend/access/rmgrdesc/xlogdesc.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,9 @@ XLogRecGetBlockRefInfo(XLogReaderState *record, bool pretty,
319319
*fpi_len += XLogRecGetBlock(record, block_id)->bimg_len;
320320

321321
if (XLogRecBlockImageApply(record, block_id))
322-
appendStringInfo(buf, " FPW");
322+
appendStringInfoString(buf, " FPW");
323323
else
324-
appendStringInfo(buf, " FPW for WAL verification");
324+
appendStringInfoString(buf, " FPW for WAL verification");
325325
}
326326
}
327327
}

Diff for: src/backend/jit/llvm/llvmjit.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ llvm_function_reference(LLVMJitContext *context,
514514
else if (basename != NULL)
515515
{
516516
/* internal function */
517-
funcname = psprintf("%s", basename);
517+
funcname = pstrdup(basename);
518518
}
519519
else
520520
{

Diff for: src/backend/libpq/hba.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ do { \
899899
errmsg("missing entry at end of line"), \
900900
errcontext("line %d of configuration file \"%s\"", \
901901
line_num, IdentFileName))); \
902-
*err_msg = psprintf("missing entry at end of line"); \
902+
*err_msg = pstrdup("missing entry at end of line"); \
903903
return NULL; \
904904
} \
905905
} while (0)
@@ -912,7 +912,7 @@ do { \
912912
errmsg("multiple values in ident field"), \
913913
errcontext("line %d of configuration file \"%s\"", \
914914
line_num, IdentFileName))); \
915-
*err_msg = psprintf("multiple values in ident field"); \
915+
*err_msg = pstrdup("multiple values in ident field"); \
916916
return NULL; \
917917
} \
918918
} while (0)

Diff for: src/backend/postmaster/postmaster.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -4457,7 +4457,7 @@ BackendInitialize(Port *port)
44574457
appendStringInfo(&ps_data, "%s ", port->user_name);
44584458
if (!am_walsender)
44594459
appendStringInfo(&ps_data, "%s ", port->database_name);
4460-
appendStringInfo(&ps_data, "%s", port->remote_host);
4460+
appendStringInfoString(&ps_data, port->remote_host);
44614461
if (port->remote_port[0] != '\0')
44624462
appendStringInfo(&ps_data, "(%s)", port->remote_port);
44634463

Diff for: src/backend/replication/logical/tablesync.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ fetch_remote_table_info(char *nspname, char *relname,
769769
foreach(lc, MySubscription->publications)
770770
{
771771
if (foreach_current_index(lc) > 0)
772-
appendStringInfo(&pub_names, ", ");
772+
appendStringInfoString(&pub_names, ", ");
773773
appendStringInfoString(&pub_names, quote_literal_cstr(strVal(lfirst(lc))));
774774
}
775775

@@ -1062,7 +1062,7 @@ copy_table(Relation rel)
10621062
appendStringInfoString(&cmd, quote_identifier(lrel.attnames[i]));
10631063
}
10641064

1065-
appendStringInfo(&cmd, ") TO STDOUT");
1065+
appendStringInfoString(&cmd, ") TO STDOUT");
10661066
}
10671067
else
10681068
{

Diff for: src/backend/utils/adt/date.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ anytime_typmodout(bool istz, int32 typmod)
9696
if (typmod >= 0)
9797
return psprintf("(%d)%s", (int) typmod, tz);
9898
else
99-
return psprintf("%s", tz);
99+
return pstrdup(tz);
100100
}
101101

102102

Diff for: src/backend/utils/adt/misc.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ pg_tablespace_databases(PG_FUNCTION_ARGS)
219219
}
220220

221221
if (tablespaceOid == DEFAULTTABLESPACE_OID)
222-
location = psprintf("base");
222+
location = "base";
223223
else
224224
location = psprintf("pg_tblspc/%u/%s", tablespaceOid,
225225
TABLESPACE_VERSION_DIRECTORY);

Diff for: src/backend/utils/adt/ruleutils.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -1453,7 +1453,7 @@ pg_get_indexdef_worker(Oid indexrelid, int colno,
14531453
appendStringInfoChar(&buf, ')');
14541454

14551455
if (idxrec->indnullsnotdistinct)
1456-
appendStringInfo(&buf, " NULLS NOT DISTINCT");
1456+
appendStringInfoString(&buf, " NULLS NOT DISTINCT");
14571457

14581458
/*
14591459
* If it has options, append "WITH (options)"
@@ -2332,9 +2332,9 @@ pg_get_constraintdef_worker(Oid constraintId, bool fullCommand,
23322332
Anum_pg_constraint_confdelsetcols, &isnull);
23332333
if (!isnull)
23342334
{
2335-
appendStringInfo(&buf, " (");
2335+
appendStringInfoString(&buf, " (");
23362336
decompile_column_index_array(val, conForm->conrelid, &buf);
2337-
appendStringInfo(&buf, ")");
2337+
appendStringInfoChar(&buf, ')');
23382338
}
23392339

23402340
break;
@@ -2363,7 +2363,7 @@ pg_get_constraintdef_worker(Oid constraintId, bool fullCommand,
23632363
((Form_pg_index) GETSTRUCT(indtup))->indnullsnotdistinct)
23642364
appendStringInfoString(&buf, "NULLS NOT DISTINCT ");
23652365

2366-
appendStringInfoString(&buf, "(");
2366+
appendStringInfoChar(&buf, '(');
23672367

23682368
/* Fetch and build target column list */
23692369
val = SysCacheGetAttr(CONSTROID, tup,
@@ -3583,7 +3583,7 @@ pg_get_function_sqlbody(PG_FUNCTION_ARGS)
35833583

35843584
ReleaseSysCache(proctup);
35853585

3586-
PG_RETURN_TEXT_P(cstring_to_text(buf.data));
3586+
PG_RETURN_TEXT_P(cstring_to_text_with_len(buf.data, buf.len));
35873587
}
35883588

35893589

Diff for: src/backend/utils/adt/timestamp.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ anytimestamp_typmodout(bool istz, int32 typmod)
131131
if (typmod >= 0)
132132
return psprintf("(%d)%s", (int) typmod, tz);
133133
else
134-
return psprintf("%s", tz);
134+
return pstrdup(tz);
135135
}
136136

137137

Diff for: src/bin/pg_amcheck/pg_amcheck.c

+23-23
Original file line numberDiff line numberDiff line change
@@ -1509,7 +1509,7 @@ append_db_pattern_cte(PQExpBuffer buf, const PatternInfoArray *pia,
15091509
have_values = true;
15101510
appendPQExpBuffer(buf, "%s\n(%d, ", comma, pattern_id);
15111511
appendStringLiteralConn(buf, info->db_regex, conn);
1512-
appendPQExpBufferStr(buf, ")");
1512+
appendPQExpBufferChar(buf, ')');
15131513
comma = ",";
15141514
}
15151515
}
@@ -1765,7 +1765,7 @@ append_rel_pattern_raw_cte(PQExpBuffer buf, const PatternInfoArray *pia,
17651765
appendPQExpBufferStr(buf, ", true::BOOLEAN");
17661766
else
17671767
appendPQExpBufferStr(buf, ", false::BOOLEAN");
1768-
appendPQExpBufferStr(buf, ")");
1768+
appendPQExpBufferChar(buf, ')');
17691769
comma = ",";
17701770
}
17711771

@@ -1972,15 +1972,15 @@ compile_relation_list_one_db(PGconn *conn, SimplePtrList *relations,
19721972
* selected above, filtering by exclusion patterns (if any) that match
19731973
* btree index names.
19741974
*/
1975-
appendPQExpBuffer(&sql,
1976-
", index (oid, nspname, relname, relpages) AS ("
1977-
"\nSELECT c.oid, r.nspname, c.relname, c.relpages "
1978-
"FROM relation r"
1979-
"\nINNER JOIN pg_catalog.pg_index i "
1980-
"ON r.oid = i.indrelid "
1981-
"INNER JOIN pg_catalog.pg_class c "
1982-
"ON i.indexrelid = c.oid "
1983-
"AND c.relpersistence != 't'");
1975+
appendPQExpBufferStr(&sql,
1976+
", index (oid, nspname, relname, relpages) AS ("
1977+
"\nSELECT c.oid, r.nspname, c.relname, c.relpages "
1978+
"FROM relation r"
1979+
"\nINNER JOIN pg_catalog.pg_index i "
1980+
"ON r.oid = i.indrelid "
1981+
"INNER JOIN pg_catalog.pg_class c "
1982+
"ON i.indexrelid = c.oid "
1983+
"AND c.relpersistence != 't'");
19841984
if (opts.excludeidx || opts.excludensp)
19851985
appendPQExpBufferStr(&sql,
19861986
"\nINNER JOIN pg_catalog.pg_namespace n "
@@ -2011,15 +2011,15 @@ compile_relation_list_one_db(PGconn *conn, SimplePtrList *relations,
20112011
* primary heap tables selected above, filtering by exclusion patterns
20122012
* (if any) that match the toast index names.
20132013
*/
2014-
appendPQExpBuffer(&sql,
2015-
", toast_index (oid, nspname, relname, relpages) AS ("
2016-
"\nSELECT c.oid, 'pg_toast', c.relname, c.relpages "
2017-
"FROM toast t "
2018-
"INNER JOIN pg_catalog.pg_index i "
2019-
"ON t.oid = i.indrelid"
2020-
"\nINNER JOIN pg_catalog.pg_class c "
2021-
"ON i.indexrelid = c.oid "
2022-
"AND c.relpersistence != 't'");
2014+
appendPQExpBufferStr(&sql,
2015+
", toast_index (oid, nspname, relname, relpages) AS ("
2016+
"\nSELECT c.oid, 'pg_toast', c.relname, c.relpages "
2017+
"FROM toast t "
2018+
"INNER JOIN pg_catalog.pg_index i "
2019+
"ON t.oid = i.indrelid"
2020+
"\nINNER JOIN pg_catalog.pg_class c "
2021+
"ON i.indexrelid = c.oid "
2022+
"AND c.relpersistence != 't'");
20232023
if (opts.excludeidx)
20242024
appendPQExpBufferStr(&sql,
20252025
"\nLEFT OUTER JOIN exclude_pat ep "
@@ -2044,9 +2044,9 @@ compile_relation_list_one_db(PGconn *conn, SimplePtrList *relations,
20442044
* matched in their own right, so we rely on UNION to deduplicate the
20452045
* list.
20462046
*/
2047-
appendPQExpBuffer(&sql,
2048-
"\nSELECT pattern_id, is_heap, is_btree, oid, nspname, relname, relpages "
2049-
"FROM (");
2047+
appendPQExpBufferStr(&sql,
2048+
"\nSELECT pattern_id, is_heap, is_btree, oid, nspname, relname, relpages "
2049+
"FROM (");
20502050
appendPQExpBufferStr(&sql,
20512051
/* Inclusion patterns that failed to match */
20522052
"\nSELECT pattern_id, is_heap, is_btree, "

0 commit comments

Comments
 (0)