diff options
Diffstat (limited to 'src/bin')
-rw-r--r-- | src/bin/initdb/initdb.c | 31 | ||||
-rw-r--r-- | src/bin/pg_ctl/pg_ctl.c | 16 | ||||
-rw-r--r-- | src/bin/pg_dump/compress_io.c | 10 | ||||
-rw-r--r-- | src/bin/pg_dump/pg_dump.c | 6 | ||||
-rw-r--r-- | src/bin/psql/command.c | 40 | ||||
-rw-r--r-- | src/bin/psql/common.c | 9 | ||||
-rw-r--r-- | src/bin/psql/copy.c | 4 | ||||
-rw-r--r-- | src/bin/psql/input.c | 6 | ||||
-rw-r--r-- | src/bin/psql/large_obj.c | 8 | ||||
-rw-r--r-- | src/bin/psql/startup.c | 14 | ||||
-rw-r--r-- | src/bin/psql/tab-complete.c | 4 |
11 files changed, 45 insertions, 103 deletions
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c index ebe8a67c0b5..f2a99ada35d 100644 --- a/src/bin/initdb/initdb.c +++ b/src/bin/initdb/initdb.c @@ -948,13 +948,10 @@ mkdatadir(const char *subdir) { char *path; - path = pg_malloc(strlen(pg_data) + 2 + - (subdir == NULL ? 0 : strlen(subdir))); - - if (subdir != NULL) - sprintf(path, "%s/%s", pg_data, subdir); + if (subdir) + pg_asprintf(&path, "%s/%s", pg_data, subdir); else - strcpy(path, pg_data); + path = pg_strdup(pg_data); if (pg_mkdir_p(path, S_IRWXU) == 0) return true; @@ -972,8 +969,7 @@ mkdatadir(const char *subdir) static void set_input(char **dest, char *filename) { - *dest = pg_malloc(strlen(share_path) + strlen(filename) + 2); - sprintf(*dest, "%s/%s", share_path, filename); + pg_asprintf(dest, "%s/%s", share_path, filename); } /* @@ -1027,15 +1023,9 @@ write_version_file(char *extrapath) char *path; if (extrapath == NULL) - { - path = pg_malloc(strlen(pg_data) + 12); - sprintf(path, "%s/PG_VERSION", pg_data); - } + pg_asprintf(&path, "%s/PG_VERSION", pg_data); else - { - path = pg_malloc(strlen(pg_data) + strlen(extrapath) + 13); - sprintf(path, "%s/%s/PG_VERSION", pg_data, extrapath); - } + pg_asprintf(&path, "%s/%s/PG_VERSION", pg_data, extrapath); if ((version_file = fopen(path, PG_BINARY_W)) == NULL) { @@ -1063,8 +1053,7 @@ set_null_conf(void) FILE *conf_file; char *path; - path = pg_malloc(strlen(pg_data) + 17); - sprintf(path, "%s/postgresql.conf", pg_data); + pg_asprintf(&path, "%s/postgresql.conf", pg_data); conf_file = fopen(path, PG_BINARY_W); if (conf_file == NULL) { @@ -2961,8 +2950,7 @@ setup_pgdata(void) * need quotes otherwise on Windows because paths there are most likely to * have embedded spaces. */ - pgdata_set_env = pg_malloc(8 + strlen(pg_data)); - sprintf(pgdata_set_env, "PGDATA=%s", pg_data); + pg_asprintf(&pgdata_set_env, "PGDATA=%s", pg_data); putenv(pgdata_set_env); } @@ -3356,8 +3344,7 @@ create_xlog_symlink(void) } /* form name of the place where the symlink must go */ - linkloc = (char *) pg_malloc(strlen(pg_data) + 8 + 1); - sprintf(linkloc, "%s/pg_xlog", pg_data); + pg_asprintf(&linkloc, "%s/pg_xlog", pg_data); #ifdef HAVE_SYMLINK if (symlink(xlog_dir, linkloc) != 0) diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c index cf50481b746..be51dc62ca7 100644 --- a/src/bin/pg_ctl/pg_ctl.c +++ b/src/bin/pg_ctl/pg_ctl.c @@ -2045,12 +2045,11 @@ main(int argc, char **argv) case 'D': { char *pgdata_D; - char *env_var = pg_malloc(strlen(optarg) + 8); + char *env_var; pgdata_D = pg_strdup(optarg); canonicalize_path(pgdata_D); - snprintf(env_var, strlen(optarg) + 8, "PGDATA=%s", - pgdata_D); + pg_asprintf(&env_var, "PGDATA=%s", pgdata_D); putenv(env_var); /* @@ -2058,10 +2057,7 @@ main(int argc, char **argv) * variable but we do -D too for clearer postmaster * 'ps' display */ - pgdata_opt = pg_malloc(strlen(pgdata_D) + 7); - snprintf(pgdata_opt, strlen(pgdata_D) + 7, - "-D \"%s\" ", - pgdata_D); + pg_asprintf(&pgdata_opt, "-D \"%s\" ", pgdata_D); break; } case 'l': @@ -2102,11 +2098,7 @@ main(int argc, char **argv) register_username = pg_strdup(optarg); else /* Prepend .\ for local accounts */ - { - register_username = pg_malloc(strlen(optarg) + 3); - strcpy(register_username, ".\\"); - strcat(register_username, optarg); - } + pg_asprintf(®ister_username, ".\\%s", optarg); break; case 'w': do_wait = true; diff --git a/src/bin/pg_dump/compress_io.c b/src/bin/pg_dump/compress_io.c index 1dd31fbe2c6..d859c8ee1e9 100644 --- a/src/bin/pg_dump/compress_io.c +++ b/src/bin/pg_dump/compress_io.c @@ -487,10 +487,9 @@ cfopen_read(const char *path, const char *mode) #ifdef HAVE_LIBZ if (fp == NULL) { - int fnamelen = strlen(path) + 4; - char *fname = pg_malloc(fnamelen); + char *fname; - snprintf(fname, fnamelen, "%s%s", path, ".gz"); + pg_asprintf(&fname, "%s.gz", path); fp = cfopen(fname, mode, 1); free(fname); } @@ -518,10 +517,9 @@ cfopen_write(const char *path, const char *mode, int compression) else { #ifdef HAVE_LIBZ - int fnamelen = strlen(path) + 4; - char *fname = pg_malloc(fnamelen); + char *fname; - snprintf(fname, fnamelen, "%s%s", path, ".gz"); + pg_asprintf(&fname, "%s.gz", path); fp = cfopen(fname, mode, 1); free(fname); #else diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 57320cc83a6..01c63b13348 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -10439,8 +10439,7 @@ convertOperatorReference(Archive *fout, const char *opr) /* If not schema-qualified, don't need to add OPERATOR() */ if (!sawdot) return name; - oname = pg_malloc(strlen(name) + 11); - sprintf(oname, "OPERATOR(%s)", name); + pg_asprintf(&oname, "OPERATOR(%s)", name); free(name); return oname; } @@ -12754,8 +12753,7 @@ dumpTable(Archive *fout, TableInfo *tbinfo) char *acltag; attnamecopy = pg_strdup(fmtId(attname)); - acltag = pg_malloc(strlen(tbinfo->dobj.name) + strlen(attname) + 2); - sprintf(acltag, "%s.%s", tbinfo->dobj.name, attname); + pg_asprintf(&acltag, "%s.%s", tbinfo->dobj.name, attname); /* Column's GRANT type is always TABLE */ dumpACL(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId, "TABLE", namecopy, attnamecopy, acltag, diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index b22e4f6190a..06ed56be683 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -1186,10 +1186,9 @@ exec_command(const char *cmd, else { /* Set variable to the value of the next argument */ - int len = strlen(envvar) + strlen(envval) + 1; - char *newval = pg_malloc(len + 1); + char *newval; - snprintf(newval, len + 1, "%s=%s", envvar, envval); + pg_asprintf(&newval, "%s=%s", envvar, envval); putenv(newval); success = true; @@ -1550,9 +1549,7 @@ prompt_for_password(const char *username) { char *prompt_text; - prompt_text = pg_malloc(strlen(username) + 100); - snprintf(prompt_text, strlen(username) + 100, - _("Password for user %s: "), username); + pg_asprintf(&prompt_text, _("Password for user %s: "), username); result = simple_prompt(prompt_text, 100, false); free(prompt_text); } @@ -1923,14 +1920,6 @@ editFile(const char *fname, int lineno) } } - /* Allocate sufficient memory for command line. */ - if (lineno > 0) - sys = pg_malloc(strlen(editorName) - + strlen(editor_lineno_arg) + 10 /* for integer */ - + 1 + strlen(fname) + 10 + 1); - else - sys = pg_malloc(strlen(editorName) + strlen(fname) + 10 + 1); - /* * On Unix the EDITOR value should *not* be quoted, since it might include * switches, eg, EDITOR="pico -t"; it's up to the user to put quotes in it @@ -1940,18 +1929,18 @@ editFile(const char *fname, int lineno) */ #ifndef WIN32 if (lineno > 0) - sprintf(sys, "exec %s %s%d '%s'", - editorName, editor_lineno_arg, lineno, fname); + pg_asprintf(&sys, "exec %s %s%d '%s'", + editorName, editor_lineno_arg, lineno, fname); else - sprintf(sys, "exec %s '%s'", - editorName, fname); + pg_asprintf(&sys, "exec %s '%s'", + editorName, fname); #else if (lineno > 0) - sprintf(sys, SYSTEMQUOTE "\"%s\" %s%d \"%s\"" SYSTEMQUOTE, + pg_asprintf(&sys, SYSTEMQUOTE "\"%s\" %s%d \"%s\"" SYSTEMQUOTE, editorName, editor_lineno_arg, lineno, fname); else - sprintf(sys, SYSTEMQUOTE "\"%s\" \"%s\"" SYSTEMQUOTE, - editorName, fname); + pg_asprintf(&sys, SYSTEMQUOTE "\"%s\" \"%s\"" SYSTEMQUOTE, + editorName, fname); #endif result = system(sys); if (result == -1) @@ -2644,14 +2633,11 @@ do_shell(const char *command) if (shellName == NULL) shellName = DEFAULT_SHELL; - sys = pg_malloc(strlen(shellName) + 16); -#ifndef WIN32 - sprintf(sys, /* See EDITOR handling comment for an explanation */ - "exec %s", shellName); +#ifndef WIN32 + pg_asprintf(&sys, "exec %s", shellName); #else - /* See EDITOR handling comment for an explanation */ - sprintf(sys, SYSTEMQUOTE "\"%s\"" SYSTEMQUOTE, shellName); + pg_asprintf(&sys, SYSTEMQUOTE "\"%s\"" SYSTEMQUOTE, shellName); #endif result = system(sys); free(sys); diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 3dea92c7d8f..71f0c8a95a8 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -594,9 +594,7 @@ StoreQueryTuple(const PGresult *result) char *value; /* concate prefix and column name */ - varname = pg_malloc(strlen(pset.gset_prefix) + strlen(colname) + 1); - strcpy(varname, pset.gset_prefix); - strcat(varname, colname); + pg_asprintf(&varname, "%s%s", pset.gset_prefix, colname); if (!PQgetisnull(result, 0, i)) value = PQgetvalue(result, 0, i); @@ -1687,10 +1685,7 @@ expand_tilde(char **filename) { char *newfn; - newfn = pg_malloc(strlen(home) + strlen(p) + 1); - strcpy(newfn, home); - strcat(newfn, p); - + pg_asprintf(&newfn, "%s%s", home, p); free(fn); *filename = newfn; } diff --git a/src/bin/psql/copy.c b/src/bin/psql/copy.c index 13123d600db..6db063ca953 100644 --- a/src/bin/psql/copy.c +++ b/src/bin/psql/copy.c @@ -79,9 +79,7 @@ xstrcat(char **var, const char *more) { char *newvar; - newvar = pg_malloc(strlen(*var) + strlen(more) + 1); - strcpy(newvar, *var); - strcat(newvar, more); + pg_asprintf(&newvar, "%s%s", *var, more); free(*var); *var = newvar; } diff --git a/src/bin/psql/input.c b/src/bin/psql/input.c index 07c9e89f368..f2b6e4ed7fa 100644 --- a/src/bin/psql/input.c +++ b/src/bin/psql/input.c @@ -298,11 +298,7 @@ initializeInput(int flags) if (histfile == NULL) { if (get_home_path(home)) - { - psql_history = pg_malloc(strlen(home) + 1 + - strlen(PSQLHISTORY) + 1); - snprintf(psql_history, MAXPGPATH, "%s/%s", home, PSQLHISTORY); - } + pg_asprintf(&psql_history, "%s/%s", home, PSQLHISTORY); } else { diff --git a/src/bin/psql/large_obj.c b/src/bin/psql/large_obj.c index faaecce6144..065e0c1cb24 100644 --- a/src/bin/psql/large_obj.c +++ b/src/bin/psql/large_obj.c @@ -200,12 +200,12 @@ do_lo_import(const char *filename_arg, const char *comment_arg) char *cmdbuf; char *bufptr; size_t slen = strlen(comment_arg); + int rv; - cmdbuf = malloc(slen * 2 + 256); - if (!cmdbuf) + rv = asprintf(&cmdbuf, "COMMENT ON LARGE OBJECT %u IS '", loid); + if (rv < 0) return fail_lo_xact("\\lo_import", own_transaction); - sprintf(cmdbuf, "COMMENT ON LARGE OBJECT %u IS '", loid); - bufptr = cmdbuf + strlen(cmdbuf); + bufptr = cmdbuf + rv; bufptr += PQescapeStringConn(pset.db, bufptr, comment_arg, slen, NULL); strcpy(bufptr, "'"); diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index dc06f667027..a45ec552f42 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -182,12 +182,8 @@ main(int argc, char *argv[]) if (options.username == NULL) password_prompt = pg_strdup(_("Password: ")); else - { - password_prompt = pg_malloc(strlen(_("Password for user %s: ")) - 2 + - strlen(options.username) + 1); - sprintf(password_prompt, _("Password for user %s: "), - options.username); - } + pg_asprintf(&password_prompt, _("Password for user %s: "), + options.username); if (pset.getPassword == TRI_YES) password = simple_prompt(password_prompt, 100, false); @@ -642,10 +638,8 @@ process_psqlrc_file(char *filename) #define R_OK 4 #endif - psqlrc_minor = pg_malloc(strlen(filename) + 1 + strlen(PG_VERSION) + 1); - sprintf(psqlrc_minor, "%s-%s", filename, PG_VERSION); - psqlrc_major = pg_malloc(strlen(filename) + 1 + strlen(PG_MAJORVERSION) + 1); - sprintf(psqlrc_major, "%s-%s", filename, PG_MAJORVERSION); + pg_asprintf(&psqlrc_minor, "%s-%s", filename, PG_VERSION); + pg_asprintf(&psqlrc_major, "%s-%s", filename, PG_MAJORVERSION); /* check for minor version first, then major, then no version */ if (access(psqlrc_minor, R_OK) == 0) diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 255061c1c41..ae8f8370f95 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -3822,7 +3822,6 @@ static char ** complete_from_variables(char *text, const char *prefix, const char *suffix) { char **matches; - int overhead = strlen(prefix) + strlen(suffix) + 1; char **varnames; int nvars = 0; int maxvars = 100; @@ -3847,8 +3846,7 @@ complete_from_variables(char *text, const char *prefix, const char *suffix) } } - buffer = (char *) pg_malloc(strlen(ptr->name) + overhead); - sprintf(buffer, "%s%s%s", prefix, ptr->name, suffix); + pg_asprintf(&buffer, "%s%s%s", prefix, ptr->name, suffix); varnames[nvars++] = buffer; } |