Skip to content

Commit e390375

Browse files
authored
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.
1 parent c6639ab commit e390375

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
@@ -3127,7 +3127,7 @@ static SQLRETURN check_catalog_name(esodbc_dbc_st *dbc, SQLWCHAR *name,
31273127
if (len < 0) {
31283128
catalog.cnt = wcslen(name);
31293129
} else {
3130-
catalog.cnt = (size_t)len;
3130+
catalog.cnt = ((size_t)len)/sizeof(SQLWCHAR);
31313131
}
31323132
if (! EQ_WSTR(&dbc->catalog, &catalog)) {
31333133
if (! dbc->catalog.cnt) {
@@ -3281,7 +3281,7 @@ SQLRETURN EsSQLSetConnectAttrW(
32813281
case SQL_ATTR_CURRENT_CATALOG:
32823282
INFOH(dbc, "setting current catalog to: `" LWPDL "`.",
32833283
/* string should be 0-term'd */
3284-
0 <= StringLength ? StringLength : SHRT_MAX,
3284+
0 <= StringLength ? StringLength/sizeof(SQLWCHAR) : SHRT_MAX,
32853285
(SQLWCHAR *)Value);
32863286
return check_catalog_name(dbc, (SQLWCHAR *)Value, StringLength);
32873287

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)