From 71e10395fd991fb95ae70c3f57250d6492fd4b60 Mon Sep 17 00:00:00 2001 From: Evan Miller Date: Tue, 11 Aug 2020 07:30:33 -0400 Subject: [PATCH] Try another SQLGetData fix Set the length of the return value to include the NUL termination charaacter. --- src/odbc/odbc.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/odbc/odbc.c b/src/odbc/odbc.c index 988dd47..d65e7f2 100644 --- a/src/odbc/odbc.c +++ b/src/odbc/odbc.c @@ -1641,21 +1641,22 @@ SQLRETURN SQL_API SQLGetData( return SQL_ERROR; } if (pcbValue) { - *pcbValue = len - stmt->pos; + *pcbValue = len + 1 - stmt->pos; } if (cbValueMax == 0) { free(str); str = NULL; return SQL_SUCCESS_WITH_INFO; } - snprintf(rgbValue, cbValueMax, "%s", str + stmt->pos); - if (stmt->pos + cbValueMax < len + 1) { - stmt->pos += cbValueMax - 1; + + memcpy(rgbValue, str + stmt->pos, MIN(len - stmt->pos, cbValueMax-1)); + ((char *)rgbValue)[MIN(len - stmt->pos, cbValueMax-1)] = '\0'; + stmt->pos += MIN(len - stmt->pos, cbValueMax-1); + if (cbValueMax - 1 < len - stmt->pos) { if (col->col_type != MDB_OLE) { free(str); str = NULL; } strcpy(sqlState, "01004"); // truncated return SQL_SUCCESS_WITH_INFO; } - stmt->pos = len; free(str); str = NULL; break;