diff options
author | David Rowley <drowley@postgresql.org> | 2022-09-06 13:19:44 +1200 |
---|---|---|
committer | David Rowley <drowley@postgresql.org> | 2022-09-06 13:19:44 +1200 |
commit | 8b26769bc441fffa8aad31dddc484c2f4043d2c9 (patch) | |
tree | bde1ddb9831a4e65880f11af9a94a52f9abbbf25 /src/backend/utils/adt | |
parent | 6bcda4a72123c3aa29fa3f03d952095675ad4468 (diff) | |
download | postgresql-8b26769bc441fffa8aad31dddc484c2f4043d2c9.tar.gz |
Fix an assortment of improper usages of string functions
In a similar effort to f736e188c and 110d81728, 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 #8 but opted to use #4 instead as I
wasn't certain enough that applying #8 was ok (e.g in hba.c)
Author: Ranier Vilela, David Rowley
Discussion: https://postgr.es/m/CAApHDvo2j2+RJBGhNtUz6BxabWWh2Jx16wMUMWKUjv70Ver1vg@mail.gmail.com
Diffstat (limited to 'src/backend/utils/adt')
-rw-r--r-- | src/backend/utils/adt/date.c | 2 | ||||
-rw-r--r-- | src/backend/utils/adt/misc.c | 2 | ||||
-rw-r--r-- | src/backend/utils/adt/ruleutils.c | 10 | ||||
-rw-r--r-- | src/backend/utils/adt/timestamp.c | 2 |
4 files changed, 8 insertions, 8 deletions
diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 081dfa2450..a2bdde0459 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -96,7 +96,7 @@ anytime_typmodout(bool istz, int32 typmod) if (typmod >= 0) return psprintf("(%d)%s", (int) typmod, tz); else - return psprintf("%s", tz); + return pstrdup(tz); } diff --git a/src/backend/utils/adt/misc.c b/src/backend/utils/adt/misc.c index d35b5d1f4f..6c45fd2007 100644 --- a/src/backend/utils/adt/misc.c +++ b/src/backend/utils/adt/misc.c @@ -219,7 +219,7 @@ pg_tablespace_databases(PG_FUNCTION_ARGS) } if (tablespaceOid == DEFAULTTABLESPACE_OID) - location = psprintf("base"); + location = "base"; else location = psprintf("pg_tblspc/%u/%s", tablespaceOid, TABLESPACE_VERSION_DIRECTORY); diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index 6594a9aac7..2b7b1b0c0f 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -1453,7 +1453,7 @@ pg_get_indexdef_worker(Oid indexrelid, int colno, appendStringInfoChar(&buf, ')'); if (idxrec->indnullsnotdistinct) - appendStringInfo(&buf, " NULLS NOT DISTINCT"); + appendStringInfoString(&buf, " NULLS NOT DISTINCT"); /* * If it has options, append "WITH (options)" @@ -2332,9 +2332,9 @@ pg_get_constraintdef_worker(Oid constraintId, bool fullCommand, Anum_pg_constraint_confdelsetcols, &isnull); if (!isnull) { - appendStringInfo(&buf, " ("); + appendStringInfoString(&buf, " ("); decompile_column_index_array(val, conForm->conrelid, &buf); - appendStringInfo(&buf, ")"); + appendStringInfoChar(&buf, ')'); } break; @@ -2363,7 +2363,7 @@ pg_get_constraintdef_worker(Oid constraintId, bool fullCommand, ((Form_pg_index) GETSTRUCT(indtup))->indnullsnotdistinct) appendStringInfoString(&buf, "NULLS NOT DISTINCT "); - appendStringInfoString(&buf, "("); + appendStringInfoChar(&buf, '('); /* Fetch and build target column list */ val = SysCacheGetAttr(CONSTROID, tup, @@ -3583,7 +3583,7 @@ pg_get_function_sqlbody(PG_FUNCTION_ARGS) ReleaseSysCache(proctup); - PG_RETURN_TEXT_P(cstring_to_text(buf.data)); + PG_RETURN_TEXT_P(cstring_to_text_with_len(buf.data, buf.len)); } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index 49cdb290ac..021b760f4e 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -131,7 +131,7 @@ anytimestamp_typmodout(bool istz, int32 typmod) if (typmod >= 0) return psprintf("(%d)%s", (int) typmod, tz); else - return psprintf("%s", tz); + return pstrdup(tz); } |