1
0
mirror of https://github.com/postgres/postgres.git synced 2025-09-09 13:09:39 +03:00

Various Coverity-spotted fixes

A number of issues were identified by the Coverity scanner and are
addressed in this patch.  None of these appear to be security issues
and many are mostly cosmetic changes.

Short comments for each of the changes follows.

Correct the semi-colon placement in be-secure.c regarding SSL retries.
Remove a useless comparison-to-NULL in proc.c (value is dereferenced
  prior to this check and therefore can't be NULL).
Add checking of chmod() return values to initdb.
Fix a couple minor memory leaks in initdb.
Fix memory leak in pg_ctl- involves free'ing the config file contents.
Use an int to capture fgetc() return instead of an enum in pg_dump.
Fix minor memory leaks in pg_dump.
  (note minor change to convertOperatorReference()'s API)
Check fclose()/remove() return codes in psql.
Check fstat(), find_my_exec() return codes in psql.
Various ECPG memory leak fixes.
Check find_my_exec() return in ECPG.
Explicitly ignore pqFlush return in libpq error-path.
Change PQfnumber() to avoid doing an strdup() when no changes required.
Remove a few useless check-against-NULL's (value deref'd beforehand).
Check rmtree(), malloc() results in pg_regress.
Also check get_alternative_expectfile() return in pg_regress.
This commit is contained in:
Stephen Frost
2014-03-01 22:14:14 -05:00
parent 9662143f0c
commit b1aebbb6a8
16 changed files with 290 additions and 73 deletions

View File

@@ -855,14 +855,22 @@ ecpg_store_input(const int lineno, const bool force_indicator, const struct vari
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);
@@ -882,14 +890,22 @@ ecpg_store_input(const int lineno, const bool force_indicator, const struct vari
}
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);
@@ -1026,7 +1042,10 @@ ecpg_store_input(const int lineno, const bool force_indicator, const struct vari
{
str = quote_postgres(PGTYPEStimestamp_to_asc(*(timestamp *) ((var + var->offset * element)->value)), quote, lineno);
if (!str)
{
ecpg_free(mallocedval);
return false;
}
slen = strlen(str);

View File

@@ -138,7 +138,11 @@ main(int argc, char *const argv[])
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)

View File

@@ -308,7 +308,11 @@ ECPGdump_a_type(FILE *o, const char *name, struct ECPGtype * type, const int bra
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,
@@ -318,37 +322,71 @@ ECPGdump_a_type(FILE *o, const char *name, struct ECPGtype * type, const int bra
}
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");
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);
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, 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");
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);
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, 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");
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);
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 : 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;
}
}

View File

@@ -437,6 +437,7 @@ remove_variable_from_list(struct arguments ** list, struct variable * var)
void
dump_variables(struct arguments * list, int mode)
{
char *str_zero = mm_strdup("0");
if (list == NULL)
return;
@@ -450,11 +451,13 @@ dump_variables(struct arguments * list, int mode)
/* 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

View File

@@ -2876,7 +2876,7 @@ closePGconn(PGconn *conn)
*/
pqPutMsgStart('X', false, conn);
pqPutMsgEnd(conn);
pqFlush(conn);
(void) pqFlush(conn);
}
/*

View File

@@ -2724,7 +2724,8 @@ PQfnumber(const PGresult *res, const char *field_name)
{
char *field_case;
bool in_quotes;
char *iptr;
bool all_lower = true;
const char *iptr;
char *optr;
int i;
@@ -2740,6 +2741,28 @@ PQfnumber(const PGresult *res, const char *field_name)
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
@@ -2883,7 +2906,7 @@ PQoidStatus(const PGresult *res)
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");
@@ -2907,7 +2930,6 @@ PQoidValue(const PGresult *res)
unsigned long result;
if (!res ||
!res->cmdStatus ||
strncmp(res->cmdStatus, "INSERT ", 7) != 0 ||
res->cmdStatus[7] < '0' ||
res->cmdStatus[7] > '9')