mirror of
https://github.com/postgres/postgres.git
synced 2025-05-14 00:03:46 -04:00
Fix a potential free() of string literal in oid2name, per report from
Michael Fuhr. Along the way, fix a bunch of accesses to uninitialized memory, add a mystrdup() routine and use it to cleanup some code.
This commit is contained in:
parent
b383c1db85
commit
e02ef267fc
@ -46,6 +46,7 @@ struct options
|
|||||||
/* function prototypes */
|
/* function prototypes */
|
||||||
void get_opts(int, char **, struct options *);
|
void get_opts(int, char **, struct options *);
|
||||||
void *myalloc(size_t size);
|
void *myalloc(size_t size);
|
||||||
|
char *mystrdup(const char *str);
|
||||||
void add_one_elt(char *eltname, eary *eary);
|
void add_one_elt(char *eltname, eary *eary);
|
||||||
char *get_comma_elts(eary *eary);
|
char *get_comma_elts(eary *eary);
|
||||||
PGconn *sql_conn(struct options *);
|
PGconn *sql_conn(struct options *);
|
||||||
@ -68,6 +69,11 @@ get_opts(int argc, char **argv, struct options * my_opts)
|
|||||||
my_opts->nodb = false;
|
my_opts->nodb = false;
|
||||||
my_opts->extended = false;
|
my_opts->extended = false;
|
||||||
my_opts->tablespaces = false;
|
my_opts->tablespaces = false;
|
||||||
|
my_opts->dbname = NULL;
|
||||||
|
my_opts->hostname = NULL;
|
||||||
|
my_opts->port = NULL;
|
||||||
|
my_opts->username = NULL;
|
||||||
|
my_opts->password = NULL;
|
||||||
|
|
||||||
/* get opts */
|
/* get opts */
|
||||||
while ((c = getopt(argc, argv, "H:p:U:P:d:t:o:f:qSxish?")) != -1)
|
while ((c = getopt(argc, argv, "H:p:U:P:d:t:o:f:qSxish?")) != -1)
|
||||||
@ -76,8 +82,7 @@ get_opts(int argc, char **argv, struct options * my_opts)
|
|||||||
{
|
{
|
||||||
/* specify the database */
|
/* specify the database */
|
||||||
case 'd':
|
case 'd':
|
||||||
my_opts->dbname = (char *) myalloc(strlen(optarg));
|
my_opts->dbname = mystrdup(optarg);
|
||||||
sscanf(optarg, "%s", my_opts->dbname);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* specify one tablename to show */
|
/* specify one tablename to show */
|
||||||
@ -102,26 +107,22 @@ get_opts(int argc, char **argv, struct options * my_opts)
|
|||||||
|
|
||||||
/* host to connect to */
|
/* host to connect to */
|
||||||
case 'H':
|
case 'H':
|
||||||
my_opts->hostname = (char *) myalloc(strlen(optarg));
|
my_opts->hostname = mystrdup(optarg);
|
||||||
sscanf(optarg, "%s", my_opts->hostname);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* port to connect to on remote host */
|
/* port to connect to on remote host */
|
||||||
case 'p':
|
case 'p':
|
||||||
my_opts->port = (char *) myalloc(strlen(optarg));
|
my_opts->port = mystrdup(optarg);
|
||||||
sscanf(optarg, "%s", my_opts->port);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* username */
|
/* username */
|
||||||
case 'U':
|
case 'U':
|
||||||
my_opts->username = (char *) myalloc(strlen(optarg));
|
my_opts->username = mystrdup(optarg);
|
||||||
sscanf(optarg, "%s", my_opts->username);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* password */
|
/* password */
|
||||||
case 'P':
|
case 'P':
|
||||||
my_opts->password = (char *) myalloc(strlen(optarg));
|
my_opts->password = mystrdup(optarg);
|
||||||
sscanf(optarg, "%s", my_opts->password);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* display system tables */
|
/* display system tables */
|
||||||
@ -183,6 +184,18 @@ myalloc(size_t size)
|
|||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *
|
||||||
|
mystrdup(const char *str)
|
||||||
|
{
|
||||||
|
char *result = strdup(str);
|
||||||
|
if (!result)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "out of memory");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* add_one_elt
|
* add_one_elt
|
||||||
*
|
*
|
||||||
@ -208,7 +221,7 @@ add_one_elt(char *eltname, eary *eary)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
eary->array[eary->num] = strdup(eltname);
|
eary->array[eary->num] = mystrdup(eltname);
|
||||||
eary->num++;
|
eary->num++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -227,7 +240,7 @@ get_comma_elts(eary *eary)
|
|||||||
int i, length = 0;
|
int i, length = 0;
|
||||||
|
|
||||||
if (eary->num == 0)
|
if (eary->num == 0)
|
||||||
return "";
|
return mystrdup("");
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* PQescapeString wants 2 * length + 1 bytes of breath space. Add two
|
* PQescapeString wants 2 * length + 1 bytes of breath space. Add two
|
||||||
|
Loading…
x
Reference in New Issue
Block a user