Skip to content

Commit a45395b

Browse files
committed
Fixes: possible buffer overrun on interval conversion. Catalog length handling (#217)
* fix overrun when converting a c-string to interval When converting a C-string to a wide string, the conversion function 0-terminates the output. However, the allocated destination buffer lacked the space for the terminator. This commit fixes that. * fix catalog setting The API function provides the byte count for the wide string name of the catalog, not the character count, as so far implemented. This commit fixes the lenght handling. (cherry picked from commit e390375)
1 parent d70c20a commit a45395b

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

driver/connect.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3105,7 +3105,7 @@ static SQLRETURN check_catalog_name(esodbc_dbc_st *dbc, SQLWCHAR *name,
31053105
if (len < 0) {
31063106
catalog.cnt = wcslen(name);
31073107
} else {
3108-
catalog.cnt = (size_t)len;
3108+
catalog.cnt = ((size_t)len)/sizeof(SQLWCHAR);
31093109
}
31103110
if (! EQ_WSTR(&dbc->catalog, &catalog)) {
31113111
if (! dbc->catalog.cnt) {
@@ -3257,7 +3257,7 @@ SQLRETURN EsSQLSetConnectAttrW(
32573257
case SQL_ATTR_CURRENT_CATALOG:
32583258
INFOH(dbc, "setting current catalog to: `" LWPDL "`.",
32593259
/* string should be 0-term'd */
3260-
0 <= StringLength ? StringLength : SHRT_MAX,
3260+
0 <= StringLength ? StringLength/sizeof(SQLWCHAR) : SHRT_MAX,
32613261
(SQLWCHAR *)Value);
32623262
return check_catalog_name(dbc, (SQLWCHAR *)Value, StringLength);
32633263

driver/convert.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4495,7 +4495,9 @@ static SQLRETURN c2sql_str2interval(esodbc_rec_st *arec, esodbc_rec_st *irec,
44954495
INFOH(stmt, "translation buffer too small (%zu < %lld), "
44964496
"allocation needed.", sizeof(wbuff)/sizeof(wbuff[0]),
44974497
(size_t)octet_len);
4498-
wptr = malloc(octet_len * sizeof(SQLWCHAR));
4498+
/* 0-term is most of the time not counted in input str and
4499+
* ascii_c2w() writes it -> always allocate space for it */
4500+
wptr = malloc((octet_len + 1) * sizeof(SQLWCHAR));
44994501
if (! wptr) {
45004502
ERRNH(stmt, "OOM for %lld x SQLWCHAR", octet_len);
45014503
RET_HDIAGS(stmt, SQL_STATE_HY001);
@@ -4514,6 +4516,8 @@ static SQLRETURN c2sql_str2interval(esodbc_rec_st *arec, esodbc_rec_st *irec,
45144516
}
45154517
/* should only happen on too short input string */
45164518
RET_HDIAGS(stmt, SQL_STATE_22018);
4519+
} else {
4520+
assert(ret <= octet_len + 1); /* no overrun */
45174521
}
45184522
wstr.str = wptr;
45194523
wstr.cnt = (size_t)octet_len;

0 commit comments

Comments
 (0)