Skip to content

Commit db3cde3

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 1c3b8ea commit db3cde3

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
@@ -2618,7 +2618,7 @@ static SQLRETURN check_catalog_name(esodbc_dbc_st *dbc, SQLWCHAR *name,
26182618
if (len < 0) {
26192619
catalog.cnt = wcslen(name);
26202620
} else {
2621-
catalog.cnt = (size_t)len;
2621+
catalog.cnt = ((size_t)len)/sizeof(SQLWCHAR);
26222622
}
26232623
if (! EQ_WSTR(&dbc->catalog, &catalog)) {
26242624
if (! dbc->catalog.cnt) {
@@ -2770,7 +2770,7 @@ SQLRETURN EsSQLSetConnectAttrW(
27702770
case SQL_ATTR_CURRENT_CATALOG:
27712771
DBGH(dbc, "setting current catalog to: `" LWPDL "`.",
27722772
/* string should be 0-term'd */
2773-
0 <= StringLength ? StringLength : SHRT_MAX,
2773+
0 <= StringLength ? StringLength/sizeof(SQLWCHAR) : SHRT_MAX,
27742774
(SQLWCHAR *)Value);
27752775
return check_catalog_name(dbc, (SQLWCHAR *)Value, StringLength);
27762776

driver/convert.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3917,7 +3917,9 @@ static SQLRETURN c2sql_str2interval(esodbc_rec_st *arec, esodbc_rec_st *irec,
39173917
INFOH(stmt, "translation buffer too small (%zu < %lld), "
39183918
"allocation needed.", sizeof(wbuff)/sizeof(wbuff[0]),
39193919
(size_t)octet_len);
3920-
wptr = malloc(octet_len * sizeof(SQLWCHAR));
3920+
/* 0-term is most of the time not counted in input str and
3921+
* ascii_c2w() writes it -> always allocate space for it */
3922+
wptr = malloc((octet_len + 1) * sizeof(SQLWCHAR));
39213923
if (! wptr) {
39223924
ERRNH(stmt, "OOM for %lld x SQLWCHAR", octet_len);
39233925
RET_HDIAGS(stmt, SQL_STATE_HY001);
@@ -3936,6 +3938,8 @@ static SQLRETURN c2sql_str2interval(esodbc_rec_st *arec, esodbc_rec_st *irec,
39363938
}
39373939
/* should only happen on too short input string */
39383940
RET_HDIAGS(stmt, SQL_STATE_22018);
3941+
} else {
3942+
assert(ret <= octet_len + 1); /* no overrun */
39393943
}
39403944
wstr.str = wptr;
39413945
wstr.cnt = (size_t)octet_len;

0 commit comments

Comments
 (0)