@@ -259,7 +259,8 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
259259	struct  connection  * this ;
260260	int 			i ,
261261				connect_params  =  0 ;
262- 	char 	   * dbname  =  name  ? ecpg_strdup (name , lineno ) : NULL ,
262+ 	bool 		alloc_failed  =  (sqlca  ==  NULL );
263+ 	char 	   * dbname  =  name  ? ecpg_strdup (name , lineno , & alloc_failed ) : NULL ,
263264			   * host  =  NULL ,
264265			   * tmp ,
265266			   * port  =  NULL ,
@@ -268,7 +269,7 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
268269	const  char  * * conn_keywords ;
269270	const  char  * * conn_values ;
270271
271- 	if  (sqlca   ==   NULL )
272+ 	if  (alloc_failed )
272273	{
273274		ecpg_raise (lineno , ECPG_OUT_OF_MEMORY ,
274275				   ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY , NULL );
@@ -297,7 +298,7 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
297298		if  (envname )
298299		{
299300			ecpg_free (dbname );
300- 			dbname  =  ecpg_strdup (envname , lineno );
301+ 			dbname  =  ecpg_strdup (envname , lineno ,  & alloc_failed );
301302		}
302303	}
303304
@@ -349,7 +350,7 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
349350				tmp  =  strrchr (dbname  +  offset , '?' );
350351				if  (tmp  !=  NULL )	/* options given */ 
351352				{
352- 					options  =  ecpg_strdup (tmp  +  1 , lineno );
353+ 					options  =  ecpg_strdup (tmp  +  1 , lineno ,  & alloc_failed );
353354					* tmp  =  '\0' ;
354355				}
355356
@@ -358,7 +359,7 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
358359				{
359360					if  (tmp [1 ] !=  '\0' ) /* non-empty database name */ 
360361					{
361- 						realname  =  ecpg_strdup (tmp  +  1 , lineno );
362+ 						realname  =  ecpg_strdup (tmp  +  1 , lineno ,  & alloc_failed );
362363						connect_params ++ ;
363364					}
364365					* tmp  =  '\0' ;
@@ -368,7 +369,7 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
368369				if  (tmp  !=  NULL )	/* port number given */ 
369370				{
370371					* tmp  =  '\0' ;
371- 					port  =  ecpg_strdup (tmp  +  1 , lineno );
372+ 					port  =  ecpg_strdup (tmp  +  1 , lineno ,  & alloc_failed );
372373					connect_params ++ ;
373374				}
374375
@@ -402,7 +403,7 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
402403				{
403404					if  (* (dbname  +  offset ) !=  '\0' )
404405					{
405- 						host  =  ecpg_strdup (dbname  +  offset , lineno );
406+ 						host  =  ecpg_strdup (dbname  +  offset , lineno ,  & alloc_failed );
406407						connect_params ++ ;
407408					}
408409				}
@@ -414,22 +415,22 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
414415			tmp  =  strrchr (dbname , ':' );
415416			if  (tmp  !=  NULL )	/* port number given */ 
416417			{
417- 				port  =  ecpg_strdup (tmp  +  1 , lineno );
418+ 				port  =  ecpg_strdup (tmp  +  1 , lineno ,  & alloc_failed );
418419				connect_params ++ ;
419420				* tmp  =  '\0' ;
420421			}
421422
422423			tmp  =  strrchr (dbname , '@' );
423424			if  (tmp  !=  NULL )	/* host name given */ 
424425			{
425- 				host  =  ecpg_strdup (tmp  +  1 , lineno );
426+ 				host  =  ecpg_strdup (tmp  +  1 , lineno ,  & alloc_failed );
426427				connect_params ++ ;
427428				* tmp  =  '\0' ;
428429			}
429430
430431			if  (strlen (dbname ) >  0 )
431432			{
432- 				realname  =  ecpg_strdup (dbname , lineno );
433+ 				realname  =  ecpg_strdup (dbname , lineno ,  & alloc_failed );
433434				connect_params ++ ;
434435			}
435436			else 
@@ -460,7 +461,18 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
460461	 */ 
461462	conn_keywords  =  (const  char  * * ) ecpg_alloc ((connect_params  +  1 ) *  sizeof (char  * ), lineno );
462463	conn_values  =  (const  char  * * ) ecpg_alloc (connect_params  *  sizeof (char  * ), lineno );
463- 	if  (conn_keywords  ==  NULL  ||  conn_values  ==  NULL )
464+ 
465+ 	/* Decide on a connection name */ 
466+ 	if  (connection_name  !=  NULL  ||  realname  !=  NULL )
467+ 	{
468+ 		this -> name  =  ecpg_strdup (connection_name  ? connection_name  : realname ,
469+ 								 lineno , & alloc_failed );
470+ 	}
471+ 	else 
472+ 		this -> name  =  NULL ;
473+ 
474+ 	/* Deal with any failed allocations above */ 
475+ 	if  (conn_keywords  ==  NULL  ||  conn_values  ==  NULL  ||  alloc_failed )
464476	{
465477		if  (host )
466478			ecpg_free (host );
@@ -476,6 +488,8 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
476488			ecpg_free (conn_keywords );
477489		if  (conn_values )
478490			ecpg_free (conn_values );
491+ 		if  (this -> name )
492+ 			ecpg_free (this -> name );
479493		free (this );
480494		return  false;
481495	}
@@ -510,17 +524,14 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
510524				ecpg_free (conn_keywords );
511525			if  (conn_values )
512526				ecpg_free (conn_values );
527+ 			if  (this -> name )
528+ 				ecpg_free (this -> name );
513529			free (this );
514530			return  false;
515531		}
516532	}
517533#endif 
518534
519- 	if  (connection_name  !=  NULL )
520- 		this -> name  =  ecpg_strdup (connection_name , lineno );
521- 	else 
522- 		this -> name  =  ecpg_strdup (realname , lineno );
523- 
524535	this -> cache_head  =  NULL ;
525536	this -> prep_stmts  =  NULL ;
526537
0 commit comments