Fix for variable-length fields using SQLGetData

See:

brianb/mdbtools#130
brianb/mdbtools#131
evanmiller/mdbtools#6
This commit is contained in:
Evan Miller 2020-08-10 21:38:08 -04:00
parent 02c5544c14
commit e330feebd6
2 changed files with 14 additions and 14 deletions

View File

@ -1634,28 +1634,28 @@ SQLRETURN SQL_API SQLGetData(
str = NULL; str = NULL;
return SQL_NO_DATA; return SQL_NO_DATA;
} }
if (pcbValue) { if (cbValueMax < 0) {
*pcbValue = len; strcpy(sqlState, "HY090"); // Invalid string or buffer length
free(str);
str = NULL;
return SQL_ERROR;
} }
if (!cbValueMax) { if (pcbValue) {
*pcbValue = len - stmt->pos;
}
if (cbValueMax == 0) {
free(str); free(str);
str = NULL; str = NULL;
return SQL_SUCCESS_WITH_INFO; return SQL_SUCCESS_WITH_INFO;
} }
if (len - stmt->pos > cbValueMax) { snprintf(rgbValue, cbValueMax, "%s", str + stmt->pos);
/* the buffer we were given is too small, so if (stmt->pos + cbValueMax < len + 1) {
truncate it to the size of the buffer */
memcpy(rgbValue, str, cbValueMax);
stmt->pos += cbValueMax - 1; stmt->pos += cbValueMax - 1;
if (col->col_type != MDB_OLE) { free(str); str = NULL; } if (col->col_type != MDB_OLE) { free(str); str = NULL; }
strcpy(sqlState, "01004"); // trunctated strcpy(sqlState, "01004"); // truncated
return SQL_SUCCESS_WITH_INFO; return SQL_SUCCESS_WITH_INFO;
} }
stmt->pos = len;
memcpy(rgbValue, str + stmt->pos, len - stmt->pos);
if (pcbValue)
*pcbValue = len - stmt->pos;
stmt->pos += len - stmt->pos;
free(str); free(str);
str = NULL; str = NULL;
break; break;

View File

@ -174,7 +174,7 @@ int i;
szSqlState, szErrorMsg); szSqlState, szErrorMsg);
return 1; return 1;
} }
SQLBindCol(hstmt, 3, SQL_CHAR, szCol1, 60, &length); SQLBindCol(hstmt, 3, SQL_CHAR, szCol1, sizeof(szCol1), &length);
//SQLBindCol(hstmt, 1, SQL_CHAR, szCol1, 60, NULL); //SQLBindCol(hstmt, 1, SQL_CHAR, szCol1, 60, NULL);
/* Execute statement with first row. */ /* Execute statement with first row. */