mirror of
				https://github.com/postgres/postgres.git
				synced 2025-11-03 09:13:20 +03:00 
			
		
		
		
	Check for out of memory when allocating sqlca.
Patch by Michael Paquier
This commit is contained in:
		@@ -1032,6 +1032,8 @@ void
 | 
				
			|||||||
ECPG_informix_reset_sqlca(void)
 | 
					ECPG_informix_reset_sqlca(void)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct sqlca_t *sqlca = ECPGget_sqlca();
 | 
						struct sqlca_t *sqlca = ECPGget_sqlca();
 | 
				
			||||||
 | 
						if (sqlca == NULL)
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	memcpy((char *) sqlca, (char *) &sqlca_init, sizeof(struct sqlca_t));
 | 
						memcpy((char *) sqlca, (char *) &sqlca_init, sizeof(struct sqlca_t));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -223,6 +223,12 @@ ECPGnoticeReceiver(void *arg, const PGresult *result)
 | 
				
			|||||||
	struct sqlca_t *sqlca = ECPGget_sqlca();
 | 
						struct sqlca_t *sqlca = ECPGget_sqlca();
 | 
				
			||||||
	int			sqlcode;
 | 
						int			sqlcode;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (sqlca == NULL)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							ecpg_log("out of memory");
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	(void) arg;					/* keep the compiler quiet */
 | 
						(void) arg;					/* keep the compiler quiet */
 | 
				
			||||||
	if (sqlstate == NULL)
 | 
						if (sqlstate == NULL)
 | 
				
			||||||
		sqlstate = ECPG_SQLSTATE_ECPG_INTERNAL_ERROR;
 | 
							sqlstate = ECPG_SQLSTATE_ECPG_INTERNAL_ERROR;
 | 
				
			||||||
@@ -278,6 +284,14 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
 | 
				
			|||||||
	const char **conn_keywords;
 | 
						const char **conn_keywords;
 | 
				
			||||||
	const char **conn_values;
 | 
						const char **conn_values;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (sqlca == NULL)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
 | 
				
			||||||
 | 
									   ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
 | 
				
			||||||
 | 
							ecpg_free(dbname);
 | 
				
			||||||
 | 
							return false;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ecpg_init_sqlca(sqlca);
 | 
						ecpg_init_sqlca(sqlca);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/*
 | 
						/*
 | 
				
			||||||
@@ -657,6 +671,13 @@ ECPGdisconnect(int lineno, const char *connection_name)
 | 
				
			|||||||
	struct sqlca_t *sqlca = ECPGget_sqlca();
 | 
						struct sqlca_t *sqlca = ECPGget_sqlca();
 | 
				
			||||||
	struct connection *con;
 | 
						struct connection *con;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (sqlca == NULL)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
 | 
				
			||||||
 | 
									   ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
 | 
				
			||||||
 | 
							return (false);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef ENABLE_THREAD_SAFETY
 | 
					#ifdef ENABLE_THREAD_SAFETY
 | 
				
			||||||
	pthread_mutex_lock(&connections_mutex);
 | 
						pthread_mutex_lock(&connections_mutex);
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -132,6 +132,13 @@ ecpg_get_data(const PGresult *results, int act_tuple, int act_field, int lineno,
 | 
				
			|||||||
	int			value_for_indicator = 0;
 | 
						int			value_for_indicator = 0;
 | 
				
			||||||
	long		log_offset;
 | 
						long		log_offset;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (sqlca == NULL)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
 | 
				
			||||||
 | 
									   ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
 | 
				
			||||||
 | 
							return (false);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/*
 | 
						/*
 | 
				
			||||||
	 * If we are running in a regression test, do not log the offset variable,
 | 
						 * If we are running in a regression test, do not log the offset variable,
 | 
				
			||||||
	 * it depends on the machine's alignment.
 | 
						 * it depends on the machine's alignment.
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -93,6 +93,13 @@ ECPGget_desc_header(int lineno, const char *desc_name, int *count)
 | 
				
			|||||||
	PGresult   *ECPGresult;
 | 
						PGresult   *ECPGresult;
 | 
				
			||||||
	struct sqlca_t *sqlca = ECPGget_sqlca();
 | 
						struct sqlca_t *sqlca = ECPGget_sqlca();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (sqlca == NULL)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
 | 
				
			||||||
 | 
									   ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
 | 
				
			||||||
 | 
							return false;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ecpg_init_sqlca(sqlca);
 | 
						ecpg_init_sqlca(sqlca);
 | 
				
			||||||
	ECPGresult = ecpg_result_by_descriptor(lineno, desc_name);
 | 
						ECPGresult = ecpg_result_by_descriptor(lineno, desc_name);
 | 
				
			||||||
	if (!ECPGresult)
 | 
						if (!ECPGresult)
 | 
				
			||||||
@@ -245,6 +252,13 @@ ECPGget_desc(int lineno, const char *desc_name, int index,...)
 | 
				
			|||||||
	struct variable data_var;
 | 
						struct variable data_var;
 | 
				
			||||||
	struct sqlca_t *sqlca = ECPGget_sqlca();
 | 
						struct sqlca_t *sqlca = ECPGget_sqlca();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (sqlca == NULL)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
 | 
				
			||||||
 | 
									   ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
 | 
				
			||||||
 | 
							return false;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	va_start(args, index);
 | 
						va_start(args, index);
 | 
				
			||||||
	ecpg_init_sqlca(sqlca);
 | 
						ecpg_init_sqlca(sqlca);
 | 
				
			||||||
	ECPGresult = ecpg_result_by_descriptor(lineno, desc_name);
 | 
						ECPGresult = ecpg_result_by_descriptor(lineno, desc_name);
 | 
				
			||||||
@@ -703,6 +717,13 @@ ECPGdeallocate_desc(int line, const char *name)
 | 
				
			|||||||
	struct descriptor *prev;
 | 
						struct descriptor *prev;
 | 
				
			||||||
	struct sqlca_t *sqlca = ECPGget_sqlca();
 | 
						struct sqlca_t *sqlca = ECPGget_sqlca();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (sqlca == NULL)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							ecpg_raise(line, ECPG_OUT_OF_MEMORY,
 | 
				
			||||||
 | 
									   ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
 | 
				
			||||||
 | 
							return false;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ecpg_init_sqlca(sqlca);
 | 
						ecpg_init_sqlca(sqlca);
 | 
				
			||||||
	for (desc = get_descriptors(), prev = NULL; desc; prev = desc, desc = desc->next)
 | 
						for (desc = get_descriptors(), prev = NULL; desc; prev = desc, desc = desc->next)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
@@ -742,6 +763,13 @@ ECPGallocate_desc(int line, const char *name)
 | 
				
			|||||||
	struct descriptor *new;
 | 
						struct descriptor *new;
 | 
				
			||||||
	struct sqlca_t *sqlca = ECPGget_sqlca();
 | 
						struct sqlca_t *sqlca = ECPGget_sqlca();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (sqlca == NULL)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							ecpg_raise(line, ECPG_OUT_OF_MEMORY,
 | 
				
			||||||
 | 
									   ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
 | 
				
			||||||
 | 
							return false;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ecpg_init_sqlca(sqlca);
 | 
						ecpg_init_sqlca(sqlca);
 | 
				
			||||||
	new = (struct descriptor *) ecpg_alloc(sizeof(struct descriptor), line);
 | 
						new = (struct descriptor *) ecpg_alloc(sizeof(struct descriptor), line);
 | 
				
			||||||
	if (!new)
 | 
						if (!new)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -14,6 +14,13 @@ ecpg_raise(int line, int code, const char *sqlstate, const char *str)
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
	struct sqlca_t *sqlca = ECPGget_sqlca();
 | 
						struct sqlca_t *sqlca = ECPGget_sqlca();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (sqlca == NULL)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							ecpg_log("out of memory");
 | 
				
			||||||
 | 
							ECPGfree_auto_mem();
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	sqlca->sqlcode = code;
 | 
						sqlca->sqlcode = code;
 | 
				
			||||||
	strncpy(sqlca->sqlstate, sqlstate, sizeof(sqlca->sqlstate));
 | 
						strncpy(sqlca->sqlstate, sqlstate, sizeof(sqlca->sqlstate));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -293,6 +300,13 @@ ecpg_raise_backend(int line, PGresult *result, PGconn *conn, int compat)
 | 
				
			|||||||
	char	   *sqlstate;
 | 
						char	   *sqlstate;
 | 
				
			||||||
	char	   *message;
 | 
						char	   *message;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (sqlca == NULL)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							ecpg_log("out of memory");
 | 
				
			||||||
 | 
							ECPGfree_auto_mem();
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (result)
 | 
						if (result)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		sqlstate = PQresultErrorField(result, PG_DIAG_SQLSTATE);
 | 
							sqlstate = PQresultErrorField(result, PG_DIAG_SQLSTATE);
 | 
				
			||||||
@@ -401,6 +415,12 @@ sqlprint(void)
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
	struct sqlca_t *sqlca = ECPGget_sqlca();
 | 
						struct sqlca_t *sqlca = ECPGget_sqlca();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (sqlca == NULL)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							ecpg_log("out of memory");
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	sqlca->sqlerrm.sqlerrmc[sqlca->sqlerrm.sqlerrml] = '\0';
 | 
						sqlca->sqlerrm.sqlerrmc[sqlca->sqlerrm.sqlerrml] = '\0';
 | 
				
			||||||
	fprintf(stderr, ecpg_gettext("SQL error: %s\n"), sqlca->sqlerrm.sqlerrmc);
 | 
						fprintf(stderr, ecpg_gettext("SQL error: %s\n"), sqlca->sqlerrm.sqlerrmc);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1440,6 +1440,13 @@ ecpg_execute(struct statement * stmt)
 | 
				
			|||||||
	if (!ecpg_check_PQresult(results, stmt->lineno, stmt->connection->connection, stmt->compat))
 | 
						if (!ecpg_check_PQresult(results, stmt->lineno, stmt->connection->connection, stmt->compat))
 | 
				
			||||||
		return (false);
 | 
							return (false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (sqlca == NULL)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							ecpg_raise(stmt->lineno, ECPG_OUT_OF_MEMORY,
 | 
				
			||||||
 | 
									   ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
 | 
				
			||||||
 | 
							return (false);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var = stmt->outlist;
 | 
						var = stmt->outlist;
 | 
				
			||||||
	switch (PQresultStatus(results))
 | 
						switch (PQresultStatus(results))
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -106,6 +106,13 @@ ecpg_init(const struct connection * con, const char *connection_name, const int
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
	struct sqlca_t *sqlca = ECPGget_sqlca();
 | 
						struct sqlca_t *sqlca = ECPGget_sqlca();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (sqlca == NULL)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							ecpg_raise(lineno, ECPG_OUT_OF_MEMORY, ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY,
 | 
				
			||||||
 | 
									   NULL);
 | 
				
			||||||
 | 
							return (false);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ecpg_init_sqlca(sqlca);
 | 
						ecpg_init_sqlca(sqlca);
 | 
				
			||||||
	if (con == NULL)
 | 
						if (con == NULL)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
@@ -143,6 +150,8 @@ ECPGget_sqlca(void)
 | 
				
			|||||||
	if (sqlca == NULL)
 | 
						if (sqlca == NULL)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		sqlca = malloc(sizeof(struct sqlca_t));
 | 
							sqlca = malloc(sizeof(struct sqlca_t));
 | 
				
			||||||
 | 
							if (sqlca == NULL)
 | 
				
			||||||
 | 
								return NULL;
 | 
				
			||||||
		ecpg_init_sqlca(sqlca);
 | 
							ecpg_init_sqlca(sqlca);
 | 
				
			||||||
		pthread_setspecific(sqlca_key, sqlca);
 | 
							pthread_setspecific(sqlca_key, sqlca);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -286,9 +295,11 @@ ecpg_log(const char *format,...)
 | 
				
			|||||||
	va_end(ap);
 | 
						va_end(ap);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* dump out internal sqlca variables */
 | 
						/* dump out internal sqlca variables */
 | 
				
			||||||
	if (ecpg_internal_regression_mode)
 | 
						if (ecpg_internal_regression_mode && sqlca != NULL)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
		fprintf(debugstream, "[NO_PID]: sqlca: code: %ld, state: %s\n",
 | 
							fprintf(debugstream, "[NO_PID]: sqlca: code: %ld, state: %s\n",
 | 
				
			||||||
				sqlca->sqlcode, sqlca->sqlstate);
 | 
									sqlca->sqlcode, sqlca->sqlstate);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	fflush(debugstream);
 | 
						fflush(debugstream);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -524,6 +535,13 @@ ECPGset_var(int number, void *pointer, int lineno)
 | 
				
			|||||||
	{
 | 
						{
 | 
				
			||||||
		struct sqlca_t *sqlca = ECPGget_sqlca();
 | 
							struct sqlca_t *sqlca = ECPGget_sqlca();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (sqlca == NULL)
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
 | 
				
			||||||
 | 
										   ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
 | 
				
			||||||
 | 
								return;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		sqlca->sqlcode = ECPG_OUT_OF_MEMORY;
 | 
							sqlca->sqlcode = ECPG_OUT_OF_MEMORY;
 | 
				
			||||||
		strncpy(sqlca->sqlstate, "YE001", sizeof(sqlca->sqlstate));
 | 
							strncpy(sqlca->sqlstate, "YE001", sizeof(sqlca->sqlstate));
 | 
				
			||||||
		snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc), "out of memory on line %d", lineno);
 | 
							snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc), "out of memory on line %d", lineno);
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user