From e5ecf92328ecd538341fddbebd3db5bc0ed20476 Mon Sep 17 00:00:00 2001 From: Metin Doslu Date: Fri, 1 Jul 2016 13:53:44 +0300 Subject: [PATCH] Add null check to SqlStateMatchesCategory() Fixes #634 --- .../distributed/utils/connection_cache.c | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/backend/distributed/utils/connection_cache.c b/src/backend/distributed/utils/connection_cache.c index 1998d7760..83eb73088 100644 --- a/src/backend/distributed/utils/connection_cache.c +++ b/src/backend/distributed/utils/connection_cache.c @@ -192,19 +192,27 @@ PurgeConnection(PGconn *connection) /* - * SqlStateMatchesCategory returns true if the given sql state is in the given - * error category. Note that we use ERRCODE_TO_CATEGORY macro to determine error - * category of the sql state and expect the caller to use the same macro for the - * error category. + * SqlStateMatchesCategory returns true if the given sql state (which may be + * NULL if unknown) is in the given error category. Note that we use + * ERRCODE_TO_CATEGORY macro to determine error category of the sql state and + * expect the caller to use the same macro for the error category. */ bool SqlStateMatchesCategory(char *sqlStateString, int category) { bool sqlStateMatchesCategory = false; - int sqlState = MAKE_SQLSTATE(sqlStateString[0], sqlStateString[1], sqlStateString[2], - sqlStateString[3], sqlStateString[4]); + int sqlState = 0; + int sqlStateCategory = 0; - int sqlStateCategory = ERRCODE_TO_CATEGORY(sqlState); + if (sqlStateString == NULL) + { + return false; + } + + sqlState = MAKE_SQLSTATE(sqlStateString[0], sqlStateString[1], sqlStateString[2], + sqlStateString[3], sqlStateString[4]); + + sqlStateCategory = ERRCODE_TO_CATEGORY(sqlState); if (sqlStateCategory == category) { sqlStateMatchesCategory = true;