Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1fbdb6b

Browse files
author
Michael Meskes
committed
Fixed segfault in connect when specifying no database name.
1 parent c3d583d commit 1fbdb6b

File tree

2 files changed

+92
-83
lines changed

2 files changed

+92
-83
lines changed

src/interfaces/ecpg/ecpglib/connect.c

Lines changed: 86 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/connect.c,v 1.23 2004/08/29 05:06:59 momjian Exp $ */
1+
/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/connect.c,v 1.24 2004/12/30 09:36:37 meskes Exp $ */
22

33
#define POSTGRES_ECPG_INTERNAL
44
#include "postgres_fe.h"
@@ -242,7 +242,7 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
242242
struct sqlca_t *sqlca = ECPGget_sqlca();
243243
enum COMPAT_MODE compat = c;
244244
struct connection *this;
245-
char *dbname = strdup(name),
245+
char *dbname = name ? strdup(name) : NULL,
246246
*host = NULL,
247247
*tmp,
248248
*port = NULL,
@@ -275,75 +275,100 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
275275
if (dbname == NULL && connection_name == NULL)
276276
connection_name = "DEFAULT";
277277

278-
/* get the detail information out of dbname */
279-
if (strchr(dbname, '@') != NULL)
280-
{
281-
/* old style: dbname[@server][:port] */
282-
tmp = strrchr(dbname, ':');
283-
if (tmp != NULL) /* port number given */
284-
{
285-
port = strdup(tmp + 1);
286-
*tmp = '\0';
287-
}
288-
289-
tmp = strrchr(dbname, '@');
290-
if (tmp != NULL) /* host name given */
291-
{
292-
host = strdup(tmp + 1);
293-
*tmp = '\0';
294-
}
295-
realname = strdup(dbname);
296-
}
297-
else if (strncmp(dbname, "tcp:", 4) == 0 || strncmp(dbname, "unix:", 5) == 0)
298-
{
299-
int offset = 0;
300-
301-
/*
302-
* only allow protocols tcp and unix
303-
*/
304-
if (strncmp(dbname, "tcp:", 4) == 0)
305-
offset = 4;
306-
else if (strncmp(dbname, "unix:", 5) == 0)
307-
offset = 5;
308-
309-
if (strncmp(dbname + offset, "postgresql://", strlen("postgresql://")) == 0)
278+
if (dbname != NULL)
279+
{
280+
/* get the detail information out of dbname */
281+
if (strchr(dbname, '@') != NULL)
310282
{
311-
312-
/*------
313-
* new style:
314-
* <tcp|unix>:postgresql://server[:port|:/unixsocket/path:]
315-
* [/db name][?options]
316-
*------
317-
*/
318-
offset += strlen("postgresql://");
319-
320-
tmp = strrchr(dbname + offset, '?');
321-
if (tmp != NULL) /* options given */
283+
/* old style: dbname[@server][:port] */
284+
tmp = strrchr(dbname, ':');
285+
if (tmp != NULL) /* port number given */
322286
{
323-
options = strdup(tmp + 1);
287+
port = strdup(tmp + 1);
324288
*tmp = '\0';
325289
}
326290

327-
tmp = last_dir_separator(dbname + offset);
328-
if (tmp != NULL) /* database name given */
291+
tmp = strrchr(dbname, '@');
292+
if (tmp != NULL) /* host name given */
329293
{
330-
realname = strdup(tmp + 1);
294+
host = strdup(tmp + 1);
331295
*tmp = '\0';
332296
}
297+
realname = strdup(dbname);
298+
}
299+
else if (strncmp(dbname, "tcp:", 4) == 0 || strncmp(dbname, "unix:", 5) == 0)
300+
{
301+
int offset = 0;
302+
303+
/*
304+
* only allow protocols tcp and unix
305+
*/
306+
if (strncmp(dbname, "tcp:", 4) == 0)
307+
offset = 4;
308+
else if (strncmp(dbname, "unix:", 5) == 0)
309+
offset = 5;
333310

334-
tmp = strrchr(dbname + offset, ':');
335-
if (tmp != NULL) /* port number or Unix socket path given */
311+
if (strncmp(dbname + offset, "postgresql://", strlen("postgresql://")) == 0)
336312
{
337-
char *tmp2;
338313

339-
*tmp = '\0';
340-
if ((tmp2 = strchr(tmp + 1, ':')) != NULL)
314+
/*------
315+
* new style:
316+
* <tcp|unix>:postgresql://server[:port|:/unixsocket/path:]
317+
* [/db name][?options]
318+
*------
319+
*/
320+
offset += strlen("postgresql://");
321+
322+
tmp = strrchr(dbname + offset, '?');
323+
if (tmp != NULL) /* options given */
324+
{
325+
options = strdup(tmp + 1);
326+
*tmp = '\0';
327+
}
328+
329+
tmp = last_dir_separator(dbname + offset);
330+
if (tmp != NULL) /* database name given */
341331
{
342-
*tmp2 = '\0';
343-
host = strdup(tmp + 1);
344-
if (strncmp(dbname, "unix:", 5) != 0)
332+
realname = strdup(tmp + 1);
333+
*tmp = '\0';
334+
}
335+
336+
tmp = strrchr(dbname + offset, ':');
337+
if (tmp != NULL) /* port number or Unix socket path given */
338+
{
339+
char *tmp2;
340+
341+
*tmp = '\0';
342+
if ((tmp2 = strchr(tmp + 1, ':')) != NULL)
345343
{
346-
ECPGlog("connect: socketname %s given for TCP connection in line %d\n", host, lineno);
344+
*tmp2 = '\0';
345+
host = strdup(tmp + 1);
346+
if (strncmp(dbname, "unix:", 5) != 0)
347+
{
348+
ECPGlog("connect: socketname %s given for TCP connection in line %d\n", host, lineno);
349+
ECPGraise(lineno, ECPG_CONNECT, ECPG_SQLSTATE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION, realname ? realname : "<DEFAULT>");
350+
if (host)
351+
ECPGfree(host);
352+
if (port)
353+
ECPGfree(port);
354+
if (options)
355+
ECPGfree(options);
356+
if (realname)
357+
ECPGfree(realname);
358+
if (dbname)
359+
ECPGfree(dbname);
360+
return false;
361+
}
362+
}
363+
else
364+
port = strdup(tmp + 1);
365+
}
366+
367+
if (strncmp(dbname, "unix:", 5) == 0)
368+
{
369+
if (strcmp(dbname + offset, "localhost") != 0 && strcmp(dbname + offset, "127.0.0.1") != 0)
370+
{
371+
ECPGlog("connect: non-localhost access via sockets in line %d\n", lineno);
347372
ECPGraise(lineno, ECPG_CONNECT, ECPG_SQLSTATE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION, realname ? realname : "<DEFAULT>");
348373
if (host)
349374
ECPGfree(host);
@@ -359,37 +384,17 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
359384
}
360385
}
361386
else
362-
port = strdup(tmp + 1);
363-
}
387+
host = strdup(dbname + offset);
364388

365-
if (strncmp(dbname, "unix:", 5) == 0)
366-
{
367-
if (strcmp(dbname + offset, "localhost") != 0 && strcmp(dbname + offset, "127.0.0.1") != 0)
368-
{
369-
ECPGlog("connect: non-localhost access via sockets in line %d\n", lineno);
370-
ECPGraise(lineno, ECPG_CONNECT, ECPG_SQLSTATE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION, realname ? realname : "<DEFAULT>");
371-
if (host)
372-
ECPGfree(host);
373-
if (port)
374-
ECPGfree(port);
375-
if (options)
376-
ECPGfree(options);
377-
if (realname)
378-
ECPGfree(realname);
379-
if (dbname)
380-
ECPGfree(dbname);
381-
return false;
382-
}
383389
}
384390
else
385-
host = strdup(dbname + offset);
386-
391+
realname = strdup(dbname);
387392
}
388393
else
389394
realname = strdup(dbname);
390395
}
391396
else
392-
realname = strdup(dbname);
397+
realname = NULL;
393398

394399
/* add connection to our list */
395400
#ifdef ENABLE_THREAD_SAFETY

src/interfaces/ecpg/ecpglib/memory.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/memory.c,v 1.5 2003/11/29 19:52:08 pgsql Exp $ */
1+
/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/memory.c,v 1.6 2004/12/30 09:36:37 meskes Exp $ */
22

33
#define POSTGRES_ECPG_INTERNAL
44
#include "postgres_fe.h"
@@ -46,8 +46,12 @@ ECPGrealloc(void *ptr, long size, int lineno)
4646
char *
4747
ECPGstrdup(const char *string, int lineno)
4848
{
49-
char *new = strdup(string);
49+
char *new;
5050

51+
if (string == NULL)
52+
return NULL;
53+
54+
new = strdup(string);
5155
if (!new)
5256
{
5357
ECPGraise(lineno, ECPG_OUT_OF_MEMORY, ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);

0 commit comments

Comments
 (0)