From 5b6d08cd2992922b667564a49f19580f11676050 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Sun, 13 Oct 2013 00:09:18 -0400 Subject: Add use of asprintf() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add asprintf(), pg_asprintf(), and psprintf() to simplify string allocation and composition. Replacement implementations taken from NetBSD. Reviewed-by: Álvaro Herrera Reviewed-by: Asif Naeem --- src/backend/commands/tablespace.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) (limited to 'src/backend/commands/tablespace.c') diff --git a/src/backend/commands/tablespace.c b/src/backend/commands/tablespace.c index 155eb7c248..ddc8ec759f 100644 --- a/src/backend/commands/tablespace.c +++ b/src/backend/commands/tablespace.c @@ -541,12 +541,11 @@ DropTableSpace(DropTableSpaceStmt *stmt) static void create_tablespace_directories(const char *location, const Oid tablespaceoid) { - char *linkloc = palloc(OIDCHARS + OIDCHARS + 1); - char *location_with_version_dir = palloc(strlen(location) + 1 + - strlen(TABLESPACE_VERSION_DIRECTORY) + 1); + char *linkloc; + char *location_with_version_dir; - sprintf(linkloc, "pg_tblspc/%u", tablespaceoid); - sprintf(location_with_version_dir, "%s/%s", location, + linkloc = psprintf("pg_tblspc/%u", tablespaceoid); + location_with_version_dir = psprintf("%s/%s", location, TABLESPACE_VERSION_DIRECTORY); /* @@ -652,9 +651,7 @@ destroy_tablespace_directories(Oid tablespaceoid, bool redo) char *subfile; struct stat st; - linkloc_with_version_dir = palloc(9 + 1 + OIDCHARS + 1 + - strlen(TABLESPACE_VERSION_DIRECTORY)); - sprintf(linkloc_with_version_dir, "pg_tblspc/%u/%s", tablespaceoid, + linkloc_with_version_dir = psprintf("pg_tblspc/%u/%s", tablespaceoid, TABLESPACE_VERSION_DIRECTORY); /* @@ -711,8 +708,7 @@ destroy_tablespace_directories(Oid tablespaceoid, bool redo) strcmp(de->d_name, "..") == 0) continue; - subfile = palloc(strlen(linkloc_with_version_dir) + 1 + strlen(de->d_name) + 1); - sprintf(subfile, "%s/%s", linkloc_with_version_dir, de->d_name); + subfile = psprintf("%s/%s", linkloc_with_version_dir, de->d_name); /* This check is just to deliver a friendlier error message */ if (!redo && !directory_is_empty(subfile)) -- cgit v1.2.1