diff options
Diffstat (limited to 'src/bin/psql')
-rw-r--r-- | src/bin/psql/command.c | 4 | ||||
-rw-r--r-- | src/bin/psql/common.c | 18 | ||||
-rw-r--r-- | src/bin/psql/common.h | 3 | ||||
-rw-r--r-- | src/bin/psql/copy.c | 2 | ||||
-rw-r--r-- | src/bin/psql/describe.c | 8 | ||||
-rw-r--r-- | src/bin/psql/print.c | 84 | ||||
-rw-r--r-- | src/bin/psql/psqlscan.l | 2 | ||||
-rw-r--r-- | src/bin/psql/startup.c | 4 |
8 files changed, 39 insertions, 86 deletions
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index b90d2f12b9b..8ccd00d75f7 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -1472,7 +1472,7 @@ prompt_for_password(const char *username) { char *prompt_text; - prompt_text = malloc(strlen(username) + 100); + prompt_text = pg_malloc(strlen(username) + 100); snprintf(prompt_text, strlen(username) + 100, _("Password for user %s: "), username); result = simple_prompt(prompt_text, 100, false); @@ -1549,7 +1549,7 @@ do_connect(char *dbname, char *user, char *host, char *port) } else if (o_conn && user && strcmp(PQuser(o_conn), user) == 0) { - password = strdup(PQpass(o_conn)); + password = pg_strdup(PQpass(o_conn)); } while (true) diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index f5bd0f6d42d..1cb30088c48 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -70,26 +70,12 @@ pg_malloc(size_t size) } void * -pg_malloc_zero(size_t size) +pg_malloc0(size_t size) { void *tmp; tmp = pg_malloc(size); - memset(tmp, 0, size); - return tmp; -} - -void * -pg_calloc(size_t nmemb, size_t size) -{ - void *tmp; - - tmp = calloc(nmemb, size); - if (!tmp) - { - psql_error("out of memory\n"); - exit(EXIT_FAILURE); - } + MemSet(tmp, 0, size); return tmp; } diff --git a/src/bin/psql/common.h b/src/bin/psql/common.h index 8037cbc0a0d..f54baab841d 100644 --- a/src/bin/psql/common.h +++ b/src/bin/psql/common.h @@ -28,8 +28,7 @@ */ extern char *pg_strdup(const char *string); extern void *pg_malloc(size_t size); -extern void *pg_malloc_zero(size_t size); -extern void *pg_calloc(size_t nmemb, size_t size); +extern void *pg_malloc0(size_t size); extern bool setQFout(const char *fname); diff --git a/src/bin/psql/copy.c b/src/bin/psql/copy.c index 22fcc5975e5..6cc1f4643c8 100644 --- a/src/bin/psql/copy.c +++ b/src/bin/psql/copy.c @@ -97,7 +97,7 @@ parse_slash_copy(const char *args) return NULL; } - result = pg_calloc(1, sizeof(struct copy_options)); + result = pg_malloc0(sizeof(struct copy_options)); result->before_tofrom = pg_strdup(""); /* initialize for appending */ diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index 35295a87726..15d02eeeff1 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -1236,12 +1236,12 @@ describeOneTableDetails(const char *schemaname, tableinfo.hastriggers = strcmp(PQgetvalue(res, 0, 4), "t") == 0; tableinfo.hasoids = strcmp(PQgetvalue(res, 0, 5), "t") == 0; tableinfo.reloptions = (pset.sversion >= 80200) ? - strdup(PQgetvalue(res, 0, 6)) : NULL; + pg_strdup(PQgetvalue(res, 0, 6)) : NULL; tableinfo.tablespace = (pset.sversion >= 80000) ? atooid(PQgetvalue(res, 0, 7)) : 0; tableinfo.reloftype = (pset.sversion >= 90000 && strcmp(PQgetvalue(res, 0, 8), "") != 0) ? - strdup(PQgetvalue(res, 0, 8)) : NULL; + pg_strdup(PQgetvalue(res, 0, 8)) : NULL; tableinfo.relpersistence = (pset.sversion >= 90100) ? *(PQgetvalue(res, 0, 9)) : 0; PQclear(res); @@ -1382,7 +1382,7 @@ describeOneTableDetails(const char *schemaname, { show_modifiers = true; headers[cols++] = gettext_noop("Modifiers"); - modifiers = pg_malloc_zero((numrows + 1) * sizeof(*modifiers)); + modifiers = pg_malloc0((numrows + 1) * sizeof(*modifiers)); } if (tableinfo.relkind == 'S') @@ -2417,7 +2417,7 @@ describeRoles(const char *pattern, bool verbose) return false; nrows = PQntuples(res); - attr = pg_malloc_zero((nrows + 1) * sizeof(*attr)); + attr = pg_malloc0((nrows + 1) * sizeof(*attr)); printTableInit(&cont, &myopt, _("List of roles"), ncols, nrows); diff --git a/src/bin/psql/print.c b/src/bin/psql/print.c index 8fa5e371284..84212450887 100644 --- a/src/bin/psql/print.c +++ b/src/bin/psql/print.c @@ -131,34 +131,6 @@ static void IsPagerNeeded(const printTableContent *cont, const int extra_lines, static void print_aligned_vertical(const printTableContent *cont, FILE *fout); -static void * -pg_local_malloc(size_t size) -{ - void *tmp; - - tmp = malloc(size); - if (!tmp) - { - fprintf(stderr, _("out of memory\n")); - exit(EXIT_FAILURE); - } - return tmp; -} - -static void * -pg_local_calloc(int count, size_t size) -{ - void *tmp; - - tmp = calloc(count, size); - if (!tmp) - { - fprintf(stderr, _("out of memory\n")); - exit(EXIT_FAILURE); - } - return tmp; -} - static int integer_digits(const char *my_str) { @@ -210,8 +182,7 @@ format_numeric_locale(const char *my_str) leading_digits; int groupdigits = atoi(grouping); int new_str_start = 0; - char *new_str = pg_local_malloc( - strlen_with_numeric_locale(my_str) + 1); + char *new_str = pg_malloc(strlen_with_numeric_locale(my_str) + 1); leading_digits = (int_len % groupdigits != 0) ? int_len % groupdigits : groupdigits; @@ -571,18 +542,18 @@ print_aligned_text(const printTableContent *cont, FILE *fout) if (cont->ncolumns > 0) { col_count = cont->ncolumns; - width_header = pg_local_calloc(col_count, sizeof(*width_header)); - width_average = pg_local_calloc(col_count, sizeof(*width_average)); - max_width = pg_local_calloc(col_count, sizeof(*max_width)); - width_wrap = pg_local_calloc(col_count, sizeof(*width_wrap)); - max_nl_lines = pg_local_calloc(col_count, sizeof(*max_nl_lines)); - curr_nl_line = pg_local_calloc(col_count, sizeof(*curr_nl_line)); - col_lineptrs = pg_local_calloc(col_count, sizeof(*col_lineptrs)); - max_bytes = pg_local_calloc(col_count, sizeof(*max_bytes)); - format_buf = pg_local_calloc(col_count, sizeof(*format_buf)); - header_done = pg_local_calloc(col_count, sizeof(*header_done)); - bytes_output = pg_local_calloc(col_count, sizeof(*bytes_output)); - wrap = pg_local_calloc(col_count, sizeof(*wrap)); + width_header = pg_malloc0(col_count * sizeof(*width_header)); + width_average = pg_malloc0(col_count * sizeof(*width_average)); + max_width = pg_malloc0(col_count * sizeof(*max_width)); + width_wrap = pg_malloc0(col_count * sizeof(*width_wrap)); + max_nl_lines = pg_malloc0(col_count * sizeof(*max_nl_lines)); + curr_nl_line = pg_malloc0(col_count * sizeof(*curr_nl_line)); + col_lineptrs = pg_malloc0(col_count * sizeof(*col_lineptrs)); + max_bytes = pg_malloc0(col_count * sizeof(*max_bytes)); + format_buf = pg_malloc0(col_count * sizeof(*format_buf)); + header_done = pg_malloc0(col_count * sizeof(*header_done)); + bytes_output = pg_malloc0(col_count * sizeof(*bytes_output)); + wrap = pg_malloc0(col_count * sizeof(*wrap)); } else { @@ -678,10 +649,10 @@ print_aligned_text(const printTableContent *cont, FILE *fout) for (i = 0; i < col_count; i++) { /* Add entry for ptr == NULL array termination */ - col_lineptrs[i] = pg_local_calloc(max_nl_lines[i] + 1, - sizeof(**col_lineptrs)); + col_lineptrs[i] = pg_malloc0((max_nl_lines[i] + 1) * + sizeof(**col_lineptrs)); - format_buf[i] = pg_local_malloc(max_bytes[i] + 1); + format_buf[i] = pg_malloc(max_bytes[i] + 1); col_lineptrs[i]->ptr = format_buf[i]; } @@ -1247,11 +1218,11 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout) * We now have all the information we need to setup the formatting * structures */ - dlineptr = pg_local_malloc((sizeof(*dlineptr)) * (dheight + 1)); - hlineptr = pg_local_malloc((sizeof(*hlineptr)) * (hheight + 1)); + dlineptr = pg_malloc((sizeof(*dlineptr)) * (dheight + 1)); + hlineptr = pg_malloc((sizeof(*hlineptr)) * (hheight + 1)); - dlineptr->ptr = pg_local_malloc(dformatsize); - hlineptr->ptr = pg_local_malloc(hformatsize); + dlineptr->ptr = pg_malloc(dformatsize); + hlineptr->ptr = pg_malloc(hformatsize); if (cont->opt->start_table) { @@ -2132,17 +2103,14 @@ printTableInit(printTableContent *const content, const printTableOpt *opt, content->ncolumns = ncolumns; content->nrows = nrows; - content->headers = pg_local_calloc(ncolumns + 1, - sizeof(*content->headers)); + content->headers = pg_malloc0((ncolumns + 1) * sizeof(*content->headers)); - content->cells = pg_local_calloc(ncolumns * nrows + 1, - sizeof(*content->cells)); + content->cells = pg_malloc0((ncolumns * nrows + 1) * sizeof(*content->cells)); content->cellmustfree = NULL; content->footers = NULL; - content->aligns = pg_local_calloc(ncolumns + 1, - sizeof(*content->align)); + content->aligns = pg_malloc0((ncolumns + 1) * sizeof(*content->align)); content->header = content->headers; content->cell = content->cells; @@ -2230,8 +2198,8 @@ printTableAddCell(printTableContent *const content, char *cell, if (mustfree) { if (content->cellmustfree == NULL) - content->cellmustfree = pg_local_calloc( - content->ncolumns * content->nrows + 1, sizeof(bool)); + content->cellmustfree = + pg_malloc0((content->ncolumns * content->nrows + 1) * sizeof(bool)); content->cellmustfree[content->cellsadded] = true; } @@ -2256,7 +2224,7 @@ printTableAddFooter(printTableContent *const content, const char *footer) { printTableFooter *f; - f = pg_local_calloc(1, sizeof(*f)); + f = pg_malloc0(sizeof(*f)); f->data = pg_strdup(footer); if (content->footers == NULL) diff --git a/src/bin/psql/psqlscan.l b/src/bin/psql/psqlscan.l index 1208c8f475c..d32a12c63c8 100644 --- a/src/bin/psql/psqlscan.l +++ b/src/bin/psql/psqlscan.l @@ -1150,7 +1150,7 @@ psql_scan_create(void) { PsqlScanState state; - state = (PsqlScanStateData *) pg_malloc_zero(sizeof(PsqlScanStateData)); + state = (PsqlScanStateData *) pg_malloc0(sizeof(PsqlScanStateData)); psql_scan_reset(state); diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index 8ba8f704218..3fb12c94522 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -188,8 +188,8 @@ main(int argc, char *argv[]) password_prompt = pg_strdup(_("Password: ")); else { - password_prompt = malloc(strlen(_("Password for user %s: ")) - 2 + - strlen(options.username) + 1); + password_prompt = pg_malloc(strlen(_("Password for user %s: ")) - 2 + + strlen(options.username) + 1); sprintf(password_prompt, _("Password for user %s: "), options.username); } |