* A handshake can fail, so be prepared to retry it, but only
* a few times.
*/
- for (retries = 0; retries++;)
+ for (retries = 0;; retries++)
{
if (SSL_do_handshake(port->ssl) > 0)
break; /* done */
* Only do it if the worker is not working to protect against Xid
* wraparound.
*/
- if ((autovac != NULL) &&
- (autovac_pgxact->vacuumFlags & PROC_IS_AUTOVACUUM) &&
+ if ((autovac_pgxact->vacuumFlags & PROC_IS_AUTOVACUUM) &&
!(autovac_pgxact->vacuumFlags & PROC_VACUUM_FOR_WRAPAROUND))
{
int pid = autovac->pid;
snprintf(path, sizeof(path), "%s/postgresql.conf", pg_data);
writefile(path, conflines);
- chmod(path, S_IRUSR | S_IWUSR);
+ if (chmod(path, S_IRUSR | S_IWUSR) != 0)
+ {
+ fprintf(stderr, _("%s: could not change permissions of \"%s\": %s\n"),
+ progname, path, strerror(errno));
+ exit_nicely();
+ }
/*
* create the automatic configuration file to store the configuration
sprintf(path, "%s/%s", pg_data, PG_AUTOCONF_FILENAME);
writefile(path, autoconflines);
- chmod(path, S_IRUSR | S_IWUSR);
+ if (chmod(path, S_IRUSR | S_IWUSR) != 0)
+ {
+ fprintf(stderr, _("%s: could not change permissions of \"%s\": %s\n"),
+ progname, path, strerror(errno));
+ exit_nicely();
+ }
free(conflines);
snprintf(path, sizeof(path), "%s/pg_hba.conf", pg_data);
writefile(path, conflines);
- chmod(path, S_IRUSR | S_IWUSR);
+ if (chmod(path, S_IRUSR | S_IWUSR) != 0)
+ {
+ fprintf(stderr, _("%s: could not change permissions of \"%s\": %s\n"),
+ progname, path, strerror(errno));
+ exit_nicely();
+ }
free(conflines);
snprintf(path, sizeof(path), "%s/pg_ident.conf", pg_data);
writefile(path, conflines);
- chmod(path, S_IRUSR | S_IWUSR);
+ if (chmod(path, S_IRUSR | S_IWUSR) != 0)
+ {
+ fprintf(stderr, _("%s: could not change permissions of \"%s\": %s\n"),
+ progname, path, strerror(errno));
+ exit_nicely();
+ }
free(conflines);
* only, so this doesn't clash with "en_US" for LATIN1, say.
*/
if (normalize_locale_name(alias, localebuf))
+ {
+ char *quoted_alias = escape_quotes(alias);
+
PG_CMD_PRINTF3("INSERT INTO tmp_pg_collation VALUES (E'%s', E'%s', %d);\n",
- escape_quotes(alias), quoted_locale, enc);
+ quoted_alias, quoted_locale, enc);
+ free(quoted_alias);
+ }
+ free(quoted_locale);
}
/* Add an SQL-standard name */
static pgpid_t get_pgpid(void);
static char **readfile(const char *path);
+static void free_readfile(char **optlines);
static int start_postmaster(void);
static void read_post_opts(void);
}
+/*
+ * Free memory allocated for optlines through readfile()
+ */
+void
+free_readfile(char **optlines)
+{
+ int i = 0;
+
+ if (!optlines)
+ return;
+
+ while (optlines[i++])
+ free(optlines[i]);
+
+ free(optlines);
+
+ return;
+}
/*
* start/test/stop routines
}
}
}
+
+ /*
+ * Free the results of readfile.
+ *
+ * This is safe to call even if optlines is NULL.
+ */
+ free_readfile(optlines);
}
/* If we have a connection string, ping the server */
if (exec_path == NULL)
exec_path = optline;
}
+
+ /* Free the results of readfile. */
+ free_readfile(optlines);
}
}
}
optlines = readfile(postopts_file);
if (optlines != NULL)
+ {
for (; *optlines != NULL; optlines++)
fputs(*optlines, stdout);
+
+ /* Free the results of readfile */
+ free_readfile(optlines);
+ }
return;
}
}
else
AH->offSize = AH->intSize;
- if ((AH->format = fgetc(fh)) == EOF)
+ if ((byteread = fgetc(fh)) == EOF)
exit_horribly(modulename, "could not read input file: %s\n", strerror(errno));
+
+ AH->format = byteread;
AH->lookahead[AH->lookaheadLen++] = AH->format;
}
else
char **argnames);
static char *format_function_signature(Archive *fout,
FuncInfo *finfo, bool honor_quotes);
-static const char *convertRegProcReference(Archive *fout,
+static char *convertRegProcReference(Archive *fout,
const char *proc);
-static const char *convertOperatorReference(Archive *fout, const char *opr);
+static char *convertOperatorReference(Archive *fout, const char *opr);
static const char *convertTSFunction(Archive *fout, Oid funcOid);
static Oid findLastBuiltinOid_V71(Archive *fout, const char *);
static Oid findLastBuiltinOid_V70(Archive *fout);
char *oprjoin;
char *oprcanmerge;
char *oprcanhash;
+ char *oprregproc;
+ char *oprref;
/* Skip if not to be dumped */
if (!oprinfo->dobj.dump || dataOnly)
oprcanmerge = PQgetvalue(res, 0, i_oprcanmerge);
oprcanhash = PQgetvalue(res, 0, i_oprcanhash);
- appendPQExpBuffer(details, " PROCEDURE = %s",
- convertRegProcReference(fout, oprcode));
+ oprregproc = convertRegProcReference(fout, oprcode);
+ if (oprregproc)
+ {
+ appendPQExpBuffer(details, " PROCEDURE = %s", oprregproc);
+ free(oprregproc);
+ }
appendPQExpBuffer(oprid, "%s (",
oprinfo->dobj.name);
else
appendPQExpBufferStr(oprid, ", NONE)");
- name = convertOperatorReference(fout, oprcom);
- if (name)
- appendPQExpBuffer(details, ",\n COMMUTATOR = %s", name);
+ oprref = convertOperatorReference(fout, oprcom);
+ if (oprref)
+ {
+ appendPQExpBuffer(details, ",\n COMMUTATOR = %s", oprref);
+ free(oprref);
+ }
- name = convertOperatorReference(fout, oprnegate);
- if (name)
- appendPQExpBuffer(details, ",\n NEGATOR = %s", name);
+ oprref = convertOperatorReference(fout, oprnegate);
+ if (oprref)
+ {
+ appendPQExpBuffer(details, ",\n NEGATOR = %s", oprref);
+ free(oprref);
+ }
if (strcmp(oprcanmerge, "t") == 0)
appendPQExpBufferStr(details, ",\n MERGES");
if (strcmp(oprcanhash, "t") == 0)
appendPQExpBufferStr(details, ",\n HASHES");
- name = convertRegProcReference(fout, oprrest);
- if (name)
- appendPQExpBuffer(details, ",\n RESTRICT = %s", name);
+ oprregproc = convertRegProcReference(fout, oprrest);
+ if (oprregproc)
+ {
+ appendPQExpBuffer(details, ",\n RESTRICT = %s", oprregproc);
+ free(oprregproc);
+ }
- name = convertRegProcReference(fout, oprjoin);
- if (name)
- appendPQExpBuffer(details, ",\n JOIN = %s", name);
+ oprregproc = convertRegProcReference(fout, oprjoin);
+ if (oprregproc)
+ {
+ appendPQExpBuffer(details, ",\n JOIN = %s", oprregproc);
+ free(oprregproc);
+ }
/*
* DROP must be fully qualified in case same name appears in pg_catalog
/*
* Convert a function reference obtained from pg_operator
*
- * Returns what to print, or NULL if function references is InvalidOid
+ * Returns allocated string of what to print, or NULL if function references
+ * is InvalidOid. Returned string is expected to be free'd by the caller.
*
* In 7.3 the input is a REGPROCEDURE display; we have to strip the
* argument-types part. In prior versions, the input is a REGPROC display.
*/
-static const char *
+static char *
convertRegProcReference(Archive *fout, const char *proc)
{
/* In all cases "-" means a null reference */
}
/* REGPROC before 7.3 does not quote its result */
- return fmtId(proc);
+ return pg_strdup(fmtId(proc));
}
/*
* Convert an operator cross-reference obtained from pg_operator
*
- * Returns what to print, or NULL to print nothing
+ * Returns an allocated string of what to print, or NULL to print nothing.
+ * Caller is responsible for free'ing result string.
*
* In 7.3 and up the input is a REGOPERATOR display; we have to strip the
* argument-types part, and add OPERATOR() decoration if the name is
* schema-qualified. In older versions, the input is just a numeric OID,
* which we search our operator list for.
*/
-static const char *
+static char *
convertOperatorReference(Archive *fout, const char *opr)
{
OprInfo *oprInfo;
opr);
return NULL;
}
- return oprInfo->dobj.name;
+ return pg_strdup(oprInfo->dobj.name);
}
/*
const char *aggtransfn;
const char *aggfinalfn;
const char *aggsortop;
+ char *aggsortconvop;
bool hypothetical;
const char *aggtranstype;
const char *aggtransspace;
{
write_msg(NULL, "WARNING: aggregate function %s could not be dumped correctly for this database version; ignored\n",
aggsig);
+
+ if (aggfullsig)
+ free(aggfullsig);
+
+ free(aggsig);
+
return;
}
aggfinalfn);
}
- aggsortop = convertOperatorReference(fout, aggsortop);
- if (aggsortop)
+ aggsortconvop = convertOperatorReference(fout, aggsortop);
+ if (aggsortconvop)
{
appendPQExpBuffer(details, ",\n SORTOP = %s",
- aggsortop);
+ aggsortconvop);
+ free(aggsortconvop);
}
if (hypothetical)
destroyPQExpBuffer(query);
destroyPQExpBuffer(delq);
+ destroyPQExpBuffer(tag);
destroyPQExpBuffer(q);
}
if (fwrite(query_buf->data, 1, ql, stream) != ql)
{
psql_error("%s: %s\n", fname, strerror(errno));
- fclose(stream);
- remove(fname);
+
+ if (fclose(stream) != 0)
+ psql_error("%s: %s\n", fname, strerror(errno));
+
+ if (remove(fname) != 0)
+ psql_error("%s: %s\n", fname, strerror(errno));
+
error = true;
}
else if (fclose(stream) != 0)
{
psql_error("%s: %s\n", fname, strerror(errno));
- remove(fname);
+ if (remove(fname) != 0)
+ psql_error("%s: %s\n", fname, strerror(errno));
error = true;
}
}
if (!options->program)
{
+ int result;
+
/* make sure the specified file is not a directory */
- fstat(fileno(copystream), &st);
- if (S_ISDIR(st.st_mode))
- {
- fclose(copystream);
+ if ((result = fstat(fileno(copystream), &st)) < 0)
+ psql_error("could not stat file: %s\n",
+ strerror(errno));
+
+ if (result == 0 && S_ISDIR(st.st_mode))
psql_error("%s: cannot copy from/to a directory\n",
options->file);
+
+ if (result < 0 || S_ISDIR(st.st_mode))
+ {
+ fclose(copystream);
free_copy_options(options);
return false;
}
char etc_path[MAXPGPATH];
char *envrc = getenv("PSQLRC");
- find_my_exec(argv0, my_exec_path);
+ if (find_my_exec(argv0, my_exec_path) < 0)
+ {
+ fprintf(stderr, _("%s: could not find own program executable\n"), argv0);
+ exit(EXIT_FAILURE);
+ }
+
get_etc_path(my_exec_path, etc_path);
snprintf(rc_file, MAXPGPATH, "%s/%s", etc_path, SYSPSQLRC);
for (element = 0; element < var->arrsize; element++)
{
+ int result;
+
nval = PGTYPESnumeric_new();
if (!nval)
return false;
if (var->type == ECPGt_numeric)
- PGTYPESnumeric_copy((numeric *) ((var + var->offset * element)->value), nval);
+ result = PGTYPESnumeric_copy((numeric *) ((var + var->offset * element)->value), nval);
else
- PGTYPESnumeric_from_decimal((decimal *) ((var + var->offset * element)->value), nval);
+ result = PGTYPESnumeric_from_decimal((decimal *) ((var + var->offset * element)->value), nval);
+
+ if (result != 0)
+ {
+ PGTYPESnumeric_free(nval);
+ return false;
+ }
str = PGTYPESnumeric_to_asc(nval, nval->dscale);
slen = strlen(str);
}
else
{
+ int result;
+
nval = PGTYPESnumeric_new();
if (!nval)
return false;
if (var->type == ECPGt_numeric)
- PGTYPESnumeric_copy((numeric *) (var->value), nval);
+ result = PGTYPESnumeric_copy((numeric *) (var->value), nval);
else
- PGTYPESnumeric_from_decimal((decimal *) (var->value), nval);
+ result = PGTYPESnumeric_from_decimal((decimal *) (var->value), nval);
+
+ if (result != 0)
+ {
+ PGTYPESnumeric_free(nval);
+ return false;
+ }
str = PGTYPESnumeric_to_asc(nval, nval->dscale);
slen = strlen(str);
{
str = quote_postgres(PGTYPEStimestamp_to_asc(*(timestamp *) ((var + var->offset * element)->value)), quote, lineno);
if (!str)
+ {
+ ecpg_free(mallocedval);
return false;
+ }
slen = strlen(str);
progname = get_progname(argv[0]);
- find_my_exec(argv[0], my_exec_path);
+ if (find_my_exec(argv[0], my_exec_path) < 0)
+ {
+ fprintf(stderr, _("%s: could not locate my own executable path\n"), argv[0]);
+ return (ILLEGAL_OPTION);
+ }
output_filename = NULL;
while ((c = getopt_long(argc, argv, "vcio:I:tD:dC:r:h?", ecpg_options, NULL)) != -1)
if (ind_type != NULL)
{
if (ind_type->type == ECPGt_NO_INDICATOR)
- ECPGdump_a_simple(o, ind_name, ind_type->type, ind_type->size, mm_strdup("-1"), NULL, ind_prefix, 0);
+ {
+ char *str_neg_one = mm_strdup("-1");
+ ECPGdump_a_simple(o, ind_name, ind_type->type, ind_type->size, str_neg_one, NULL, ind_prefix, 0);
+ free(str_neg_one);
+ }
else
{
ECPGdump_a_simple(o, ind_name, ind_type->u.element->type,
}
break;
case ECPGt_struct:
- if (indicator_set && ind_type->type != ECPGt_struct)
- mmfatal(INDICATOR_NOT_STRUCT, "indicator for struct has to be a struct");
+ {
+ char *str_one = mm_strdup("1");
- ECPGdump_a_struct(o, name, ind_name, mm_strdup("1"), type, ind_type, prefix, ind_prefix);
+ if (indicator_set && ind_type->type != ECPGt_struct)
+ mmfatal(INDICATOR_NOT_STRUCT, "indicator for struct has to be a struct");
+
+ ECPGdump_a_struct(o, name, ind_name, str_one, type, ind_type, prefix, ind_prefix);
+ free(str_one);
+ }
break;
case ECPGt_union: /* cannot dump a complete union */
base_yyerror("type of union has to be specified");
break;
case ECPGt_char_variable:
- if (indicator_set && (ind_type->type == ECPGt_struct || ind_type->type == ECPGt_array))
- mmfatal(INDICATOR_NOT_SIMPLE, "indicator for simple data type has to be simple");
+ {
+ /* Allocate for each, as there are code-paths where the values get stomped on. */
+ char *str_varchar_one = mm_strdup("1");
+ char *str_arr_one = mm_strdup("1");
+ char *str_neg_one = mm_strdup("-1");
+
+ if (indicator_set && (ind_type->type == ECPGt_struct || ind_type->type == ECPGt_array))
+ mmfatal(INDICATOR_NOT_SIMPLE, "indicator for simple data type has to be simple");
- ECPGdump_a_simple(o, name, type->type, mm_strdup("1"), (arr_str_siz && strcmp(arr_str_siz, "0") != 0) ? arr_str_siz : mm_strdup("1"), struct_sizeof, prefix, 0);
- if (ind_type != NULL)
- ECPGdump_a_simple(o, ind_name, ind_type->type, ind_type->size, (arr_str_siz && strcmp(arr_str_siz, "0") != 0) ? arr_str_siz : mm_strdup("-1"), ind_struct_sizeof, ind_prefix, 0);
+ ECPGdump_a_simple(o, name, type->type, str_varchar_one, (arr_str_siz && strcmp(arr_str_siz, "0") != 0) ? arr_str_siz : str_arr_one, struct_sizeof, prefix, 0);
+ if (ind_type != NULL)
+ ECPGdump_a_simple(o, ind_name, ind_type->type, ind_type->size, (arr_str_siz && strcmp(arr_str_siz, "0") != 0) ? arr_str_siz : str_neg_one, ind_struct_sizeof, ind_prefix, 0);
+
+ free(str_varchar_one);
+ free(str_arr_one);
+ free(str_neg_one);
+ }
break;
case ECPGt_descriptor:
- if (indicator_set && (ind_type->type == ECPGt_struct || ind_type->type == ECPGt_array))
- mmfatal(INDICATOR_NOT_SIMPLE, "indicator for simple data type has to be simple");
+ {
+ /* Allocate for each, as there are code-paths where the values get stomped on. */
+ char *str_neg_one = mm_strdup("-1");
+ char *ind_type_neg_one = mm_strdup("-1");
+
+ if (indicator_set && (ind_type->type == ECPGt_struct || ind_type->type == ECPGt_array))
+ mmfatal(INDICATOR_NOT_SIMPLE, "indicator for simple data type has to be simple");
- ECPGdump_a_simple(o, name, type->type, NULL, mm_strdup("-1"), NULL, prefix, 0);
- if (ind_type != NULL)
- ECPGdump_a_simple(o, ind_name, ind_type->type, ind_type->size, mm_strdup("-1"), NULL, ind_prefix, 0);
+ ECPGdump_a_simple(o, name, type->type, NULL, str_neg_one, NULL, prefix, 0);
+ if (ind_type != NULL)
+ ECPGdump_a_simple(o, ind_name, ind_type->type, ind_type->size, ind_type_neg_one, NULL, ind_prefix, 0);
+
+ free(str_neg_one);
+ free(ind_type_neg_one);
+ }
break;
default:
- if (indicator_set && (ind_type->type == ECPGt_struct || ind_type->type == ECPGt_array))
- mmfatal(INDICATOR_NOT_SIMPLE, "indicator for simple data type has to be simple");
+ {
+ /* Allocate for each, as there are code-paths where the values get stomped on. */
+ char *str_neg_one = mm_strdup("-1");
+ char *ind_type_neg_one = mm_strdup("-1");
+
+ if (indicator_set && (ind_type->type == ECPGt_struct || ind_type->type == ECPGt_array))
+ mmfatal(INDICATOR_NOT_SIMPLE, "indicator for simple data type has to be simple");
- ECPGdump_a_simple(o, name, type->type, type->size, (arr_str_siz && strcmp(arr_str_siz, "0") != 0) ? arr_str_siz : mm_strdup("-1"), struct_sizeof, prefix, type->counter);
- if (ind_type != NULL)
- ECPGdump_a_simple(o, ind_name, ind_type->type, ind_type->size, (arr_str_siz && strcmp(arr_str_siz, "0") != 0) ? arr_str_siz : mm_strdup("-1"), ind_struct_sizeof, ind_prefix, 0);
+ ECPGdump_a_simple(o, name, type->type, type->size, (arr_str_siz && strcmp(arr_str_siz, "0") != 0) ? arr_str_siz : str_neg_one, struct_sizeof, prefix, type->counter);
+ if (ind_type != NULL)
+ ECPGdump_a_simple(o, ind_name, ind_type->type, ind_type->size, (arr_str_siz && strcmp(arr_str_siz, "0") != 0) ? arr_str_siz : ind_type_neg_one, ind_struct_sizeof, ind_prefix, 0);
+
+ free(str_neg_one);
+ free(ind_type_neg_one);
+ }
break;
}
}
void
dump_variables(struct arguments * list, int mode)
{
+ char *str_zero = mm_strdup("0");
if (list == NULL)
return;
/* Then the current element and its indicator */
ECPGdump_a_type(yyout, list->variable->name, list->variable->type, list->variable->brace_level,
list->indicator->name, list->indicator->type, list->indicator->brace_level,
- NULL, NULL, mm_strdup("0"), NULL, NULL);
+ NULL, NULL, str_zero, NULL, NULL);
/* Then release the list element. */
if (mode != 0)
free(list);
+
+ free(str_zero);
}
void
*/
pqPutMsgStart('X', false, conn);
pqPutMsgEnd(conn);
- pqFlush(conn);
+ (void) pqFlush(conn);
}
/*
{
char *field_case;
bool in_quotes;
- char *iptr;
+ bool all_lower = true;
+ const char *iptr;
char *optr;
int i;
res->attDescs == NULL)
return -1;
+ /*
+ * Check if we can avoid the strdup() and related work because the
+ * passed-in string wouldn't be changed before we do the check anyway.
+ */
+ for (iptr = field_name; *iptr; iptr++)
+ {
+ char c = *iptr;
+
+ if (c == '"' || c != pg_tolower((unsigned char) c))
+ {
+ all_lower = false;
+ break;
+ }
+ }
+
+ if (all_lower)
+ for (i = 0; i < res->numAttributes; i++)
+ if (strcmp(field_name, res->attDescs[i].name) == 0)
+ return i;
+
+ /* Fall through to the normal check if that didn't work out. */
+
/*
* Note: this code will not reject partially quoted strings, eg
* foo"BAR"foo will become fooBARfoo when it probably ought to be an error
size_t len;
- if (!res || !res->cmdStatus || strncmp(res->cmdStatus, "INSERT ", 7) != 0)
+ if (!res || strncmp(res->cmdStatus, "INSERT ", 7) != 0)
return "";
len = strspn(res->cmdStatus + 7, "0123456789");
unsigned long result;
if (!res ||
- !res->cmdStatus ||
strncmp(res->cmdStatus, "INSERT ", 7) != 0 ||
res->cmdStatus[7] < '0' ||
res->cmdStatus[7] > '9')
* Windows. See pgsql-hackers discussion of 2008-01-18.
*/
if (directory_exists(testtablespace))
- rmtree(testtablespace, true);
+ if (!rmtree(testtablespace, true))
+ {
+ fprintf(stderr, _("\n%s: could not remove test tablespace \"%s\": %s\n"),
+ progname, testtablespace, strerror(errno));
+ exit(2);
+ }
make_directory(testtablespace);
#endif
char *tmp = (char *) malloc(ssize);
char *s = (char *) malloc(ssize);
+ if (!tmp || !s)
+ return NULL;
+
strcpy(tmp, expectfile);
last_dot = strrchr(tmp, '.');
if (!last_dot)
char *alt_expectfile;
alt_expectfile = get_alternative_expectfile(expectfile, i);
+ if (!alt_expectfile)
+ {
+ fprintf(stderr, _("Unable to check secondary comparison files: %s\n"),
+ strerror(errno));
+ exit(2);
+ }
+
if (!file_exists(alt_expectfile))
+ {
+ free(alt_expectfile);
continue;
+ }
snprintf(cmd, sizeof(cmd),
SYSTEMQUOTE "diff %s \"%s\" \"%s\" > \"%s\"" SYSTEMQUOTE,
if (run_diff(cmd, diff) == 0)
{
unlink(diff);
+ free(alt_expectfile);
return false;
}
if (directory_exists(temp_install))
{
header(_("removing existing temp installation"));
- rmtree(temp_install, true);
+ if (!rmtree(temp_install, true))
+ {
+ fprintf(stderr, _("\n%s: could not remove temp installation \"%s\": %s\n"), progname, temp_install, strerror(errno));
+ exit(2);
+ }
}
header(_("creating temporary installation"));