summaryrefslogtreecommitdiff
path: root/src/backend/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend/utils')
-rw-r--r--src/backend/utils/adt/cash.c22
-rw-r--r--src/backend/utils/adt/dbsize.c28
-rw-r--r--src/backend/utils/adt/misc.c13
-rw-r--r--src/backend/utils/fmgr/dfmgr.c12
-rw-r--r--src/backend/utils/fmgr/fmgr.c5
-rw-r--r--src/backend/utils/init/miscinit.c8
-rw-r--r--src/backend/utils/misc/guc.c3
-rw-r--r--src/backend/utils/mmgr/mcxt.c49
8 files changed, 74 insertions, 66 deletions
diff --git a/src/backend/utils/adt/cash.c b/src/backend/utils/adt/cash.c
index 82551c5f30e..015875875be 100644
--- a/src/backend/utils/adt/cash.c
+++ b/src/backend/utils/adt/cash.c
@@ -377,18 +377,16 @@ cash_out(PG_FUNCTION_ARGS)
* from the value.
*----------
*/
- result = palloc(strlen(bufptr) + strlen(csymbol) + strlen(signsymbol) + 4);
-
switch (sign_posn)
{
case 0:
if (cs_precedes)
- sprintf(result, "(%s%s%s)",
+ result = psprintf("(%s%s%s)",
csymbol,
(sep_by_space == 1) ? " " : "",
bufptr);
else
- sprintf(result, "(%s%s%s)",
+ result = psprintf("(%s%s%s)",
bufptr,
(sep_by_space == 1) ? " " : "",
csymbol);
@@ -396,14 +394,14 @@ cash_out(PG_FUNCTION_ARGS)
case 1:
default:
if (cs_precedes)
- sprintf(result, "%s%s%s%s%s",
+ result = psprintf("%s%s%s%s%s",
signsymbol,
(sep_by_space == 2) ? " " : "",
csymbol,
(sep_by_space == 1) ? " " : "",
bufptr);
else
- sprintf(result, "%s%s%s%s%s",
+ result = psprintf("%s%s%s%s%s",
signsymbol,
(sep_by_space == 2) ? " " : "",
bufptr,
@@ -412,14 +410,14 @@ cash_out(PG_FUNCTION_ARGS)
break;
case 2:
if (cs_precedes)
- sprintf(result, "%s%s%s%s%s",
+ result = psprintf("%s%s%s%s%s",
csymbol,
(sep_by_space == 1) ? " " : "",
bufptr,
(sep_by_space == 2) ? " " : "",
signsymbol);
else
- sprintf(result, "%s%s%s%s%s",
+ result = psprintf("%s%s%s%s%s",
bufptr,
(sep_by_space == 1) ? " " : "",
csymbol,
@@ -428,14 +426,14 @@ cash_out(PG_FUNCTION_ARGS)
break;
case 3:
if (cs_precedes)
- sprintf(result, "%s%s%s%s%s",
+ result = psprintf("%s%s%s%s%s",
signsymbol,
(sep_by_space == 2) ? " " : "",
csymbol,
(sep_by_space == 1) ? " " : "",
bufptr);
else
- sprintf(result, "%s%s%s%s%s",
+ result = psprintf("%s%s%s%s%s",
bufptr,
(sep_by_space == 1) ? " " : "",
signsymbol,
@@ -444,14 +442,14 @@ cash_out(PG_FUNCTION_ARGS)
break;
case 4:
if (cs_precedes)
- sprintf(result, "%s%s%s%s%s",
+ result = psprintf("%s%s%s%s%s",
csymbol,
(sep_by_space == 2) ? " " : "",
signsymbol,
(sep_by_space == 1) ? " " : "",
bufptr);
else
- sprintf(result, "%s%s%s%s%s",
+ result = psprintf("%s%s%s%s%s",
bufptr,
(sep_by_space == 1) ? " " : "",
csymbol,
diff --git a/src/backend/utils/adt/dbsize.c b/src/backend/utils/adt/dbsize.c
index 868474680df..08ea4570017 100644
--- a/src/backend/utils/adt/dbsize.c
+++ b/src/backend/utils/adt/dbsize.c
@@ -627,18 +627,14 @@ pg_size_pretty_numeric(PG_FUNCTION_ARGS)
Numeric size = PG_GETARG_NUMERIC(0);
Numeric limit,
limit2;
- char *buf,
- *result;
+ char *result;
limit = int64_to_numeric(10 * 1024);
limit2 = int64_to_numeric(10 * 1024 * 2 - 1);
if (numeric_is_less(size, limit))
{
- buf = numeric_to_cstring(size);
- result = palloc(strlen(buf) + 7);
- strcpy(result, buf);
- strcat(result, " bytes");
+ result = psprintf("%s bytes", numeric_to_cstring(size));
}
else
{
@@ -650,10 +646,7 @@ pg_size_pretty_numeric(PG_FUNCTION_ARGS)
{
/* size = (size + 1) / 2 */
size = numeric_plus_one_over_two(size);
- buf = numeric_to_cstring(size);
- result = palloc(strlen(buf) + 4);
- strcpy(result, buf);
- strcat(result, " kB");
+ result = psprintf("%s kB", numeric_to_cstring(size));
}
else
{
@@ -663,10 +656,7 @@ pg_size_pretty_numeric(PG_FUNCTION_ARGS)
{
/* size = (size + 1) / 2 */
size = numeric_plus_one_over_two(size);
- buf = numeric_to_cstring(size);
- result = palloc(strlen(buf) + 4);
- strcpy(result, buf);
- strcat(result, " MB");
+ result = psprintf("%s MB", numeric_to_cstring(size));
}
else
{
@@ -677,10 +667,7 @@ pg_size_pretty_numeric(PG_FUNCTION_ARGS)
{
/* size = (size + 1) / 2 */
size = numeric_plus_one_over_two(size);
- buf = numeric_to_cstring(size);
- result = palloc(strlen(buf) + 4);
- strcpy(result, buf);
- strcat(result, " GB");
+ result = psprintf("%s GB", numeric_to_cstring(size));
}
else
{
@@ -688,10 +675,7 @@ pg_size_pretty_numeric(PG_FUNCTION_ARGS)
size = numeric_shift_right(size, 10);
/* size = (size + 1) / 2 */
size = numeric_plus_one_over_two(size);
- buf = numeric_to_cstring(size);
- result = palloc(strlen(buf) + 4);
- strcpy(result, buf);
- strcat(result, " TB");
+ result = psprintf("%s TB", numeric_to_cstring(size));
}
}
}
diff --git a/src/backend/utils/adt/misc.c b/src/backend/utils/adt/misc.c
index aecdcd056cd..63a991631de 100644
--- a/src/backend/utils/adt/misc.c
+++ b/src/backend/utils/adt/misc.c
@@ -242,11 +242,6 @@ pg_tablespace_databases(PG_FUNCTION_ARGS)
fctx = palloc(sizeof(ts_db_fctx));
- /*
- * size = tablespace dirname length + dir sep char + oid + terminator
- */
- fctx->location = (char *) palloc(9 + 1 + OIDCHARS + 1 +
- strlen(TABLESPACE_VERSION_DIRECTORY) + 1);
if (tablespaceOid == GLOBALTABLESPACE_OID)
{
fctx->dirdesc = NULL;
@@ -256,9 +251,9 @@ pg_tablespace_databases(PG_FUNCTION_ARGS)
else
{
if (tablespaceOid == DEFAULTTABLESPACE_OID)
- sprintf(fctx->location, "base");
+ fctx->location = psprintf("base");
else
- sprintf(fctx->location, "pg_tblspc/%u/%s", tablespaceOid,
+ fctx->location = psprintf("pg_tblspc/%u/%s", tablespaceOid,
TABLESPACE_VERSION_DIRECTORY);
fctx->dirdesc = AllocateDir(fctx->location);
@@ -297,9 +292,7 @@ pg_tablespace_databases(PG_FUNCTION_ARGS)
/* if database subdir is empty, don't report tablespace as used */
- /* size = path length + dir sep char + file name + terminator */
- subdir = palloc(strlen(fctx->location) + 1 + strlen(de->d_name) + 1);
- sprintf(subdir, "%s/%s", fctx->location, de->d_name);
+ subdir = psprintf("%s/%s", fctx->location, de->d_name);
dirdesc = AllocateDir(subdir);
while ((de = ReadDir(dirdesc, subdir)) != NULL)
{
diff --git a/src/backend/utils/fmgr/dfmgr.c b/src/backend/utils/fmgr/dfmgr.c
index 562a7c9ab0c..2dd9f750562 100644
--- a/src/backend/utils/fmgr/dfmgr.c
+++ b/src/backend/utils/fmgr/dfmgr.c
@@ -503,9 +503,7 @@ expand_dynamic_library_name(const char *name)
pfree(full);
}
- new = palloc(strlen(name) + strlen(DLSUFFIX) + 1);
- strcpy(new, name);
- strcat(new, DLSUFFIX);
+ new = psprintf("%s%s", name, DLSUFFIX);
if (!have_slash)
{
@@ -554,7 +552,6 @@ static char *
substitute_libpath_macro(const char *name)
{
const char *sep_ptr;
- char *ret;
AssertArg(name != NULL);
@@ -572,12 +569,7 @@ substitute_libpath_macro(const char *name)
errmsg("invalid macro name in dynamic library path: %s",
name)));
- ret = palloc(strlen(pkglib_path) + strlen(sep_ptr) + 1);
-
- strcpy(ret, pkglib_path);
- strcat(ret, sep_ptr);
-
- return ret;
+ return psprintf("%s%s", pkglib_path, sep_ptr);
}
diff --git a/src/backend/utils/fmgr/fmgr.c b/src/backend/utils/fmgr/fmgr.c
index 42de04c60a8..9246a00cbae 100644
--- a/src/backend/utils/fmgr/fmgr.c
+++ b/src/backend/utils/fmgr/fmgr.c
@@ -448,10 +448,7 @@ fetch_finfo_record(void *filehandle, char *funcname)
const Pg_finfo_record *inforec;
static Pg_finfo_record default_inforec = {0};
- /* Compute name of info func */
- infofuncname = (char *) palloc(strlen(funcname) + 10);
- strcpy(infofuncname, "pg_finfo_");
- strcat(infofuncname, funcname);
+ infofuncname = psprintf("pg_finfo_%s", funcname);
/* Try to look up the info function */
infofunc = (PGFInfoFunction) lookup_external_function(filehandle,
diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c
index ed514f61280..381a629334b 100644
--- a/src/backend/utils/init/miscinit.c
+++ b/src/backend/utils/init/miscinit.c
@@ -165,12 +165,10 @@ make_absolute_path(const char *path)
}
}
- new = malloc(strlen(buf) + strlen(path) + 2);
- if (!new)
+ if (asprintf(&new, "%s/%s", buf, path) < 0)
ereport(FATAL,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory")));
- sprintf(new, "%s/%s", buf, path);
free(buf);
}
else
@@ -1286,9 +1284,7 @@ load_libraries(const char *libraries, const char *gucname, bool restricted)
{
char *expanded;
- expanded = palloc(strlen("$libdir/plugins/") + strlen(filename) + 1);
- strcpy(expanded, "$libdir/plugins/");
- strcat(expanded, filename);
+ expanded = psprintf("$libdir/plugins/%s", filename);
pfree(filename);
filename = expanded;
}
diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
index 8db8b3f6ecf..dfc6704fd4d 100644
--- a/src/backend/utils/misc/guc.c
+++ b/src/backend/utils/misc/guc.c
@@ -7938,8 +7938,7 @@ GUCArrayAdd(ArrayType *array, const char *name, const char *value)
name = record->name;
/* build new item for array */
- newval = palloc(strlen(name) + 1 + strlen(value) + 1);
- sprintf(newval, "%s=%s", name, value);
+ newval = psprintf("%s=%s", name, value);
datum = CStringGetTextDatum(newval);
if (array)
diff --git a/src/backend/utils/mmgr/mcxt.c b/src/backend/utils/mmgr/mcxt.c
index 9574fd3c7a3..b7beb130ea3 100644
--- a/src/backend/utils/mmgr/mcxt.c
+++ b/src/backend/utils/mmgr/mcxt.c
@@ -852,3 +852,52 @@ pnstrdup(const char *in, Size len)
out[len] = '\0';
return out;
}
+
+/*
+ * asprintf()-like functions around palloc, adapted from
+ * http://ftp.netbsd.org/pub/pkgsrc/current/pkgsrc/pkgtools/libnbcompat/files/asprintf.c
+ */
+
+char *
+psprintf(const char *format, ...)
+{
+ va_list ap;
+ char *retval;
+
+ va_start(ap, format);
+ retval = pvsprintf(format, ap);
+ va_end(ap);
+
+ return retval;
+}
+
+char *
+pvsprintf(const char *format, va_list ap)
+{
+ char *buf, *new_buf;
+ size_t len;
+ int retval;
+ va_list ap2;
+
+ len = 128;
+ buf = palloc(len);
+
+ va_copy(ap2, ap);
+ retval = vsnprintf(buf, len, format, ap);
+ Assert(retval >= 0);
+
+ if (retval < len)
+ {
+ new_buf = repalloc(buf, retval + 1);
+ va_end(ap2);
+ return new_buf;
+ }
+
+ len = (size_t)retval + 1;
+ pfree(buf);
+ buf = palloc(len);
+ retval = vsnprintf(buf, len, format, ap2);
+ va_end(ap2);
+ Assert(retval == len - 1);
+ return buf;
+}