Skip to content

Commit

Permalink
fix CBOR request length estimation (#181)
Browse files Browse the repository at this point in the history
Fix a c&p error that uses the wrong parameter sizes for estimating the
length of a CBOR request.

(cherry picked from commit 6e977cd)
  • Loading branch information
bpintea committed Aug 30, 2019
1 parent 1826ff0 commit 0c030e3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
6 changes: 3 additions & 3 deletions driver/queries.c
Original file line number Diff line number Diff line change
Expand Up @@ -2966,13 +2966,13 @@ static SQLRETURN statement_len_cbor(esodbc_stmt_st *stmt, size_t *enc_len,
(*keys) ++;
}
/* "field_multi_value_leniency": true/false */
bodylen += cbor_str_obj_len(sizeof(REQ_KEY_MODE) - 1);
bodylen += cbor_str_obj_len(sizeof(REQ_KEY_MULTIVAL) - 1);
bodylen += CBOR_OBJ_BOOL_LEN;
/* "index_include_frozen": true/false */
bodylen += cbor_str_obj_len(sizeof(REQ_KEY_MODE) - 1);
bodylen += cbor_str_obj_len(sizeof(REQ_KEY_IDX_FROZEN) - 1);
bodylen += CBOR_OBJ_BOOL_LEN;
/* "time_zone": "-05:45" */
bodylen += cbor_str_obj_len(sizeof(REQ_KEY_MODE) - 1);
bodylen += cbor_str_obj_len(sizeof(REQ_KEY_TIMEZONE) - 1);
bodylen += cbor_str_obj_len(tz_param.cnt); /* lax len */
*keys += 3; /* field_m._val., idx._inc._frozen, time_zone */
}
Expand Down
20 changes: 20 additions & 0 deletions test/test_queries.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,26 @@ TEST_F(Queries, attach_error_non_sql) {
ASSERT_EQ(HDRH(stmt)->diag.native_code, SRC_AID3);
}

TEST_F(Queries, cbor_serialize_alloc_body) {
const size_t bsz = 64;
wchar_t buff[bsz];
wstr_st select = WSTR_INIT("SELECT '");
cstr_st body = {(SQLCHAR *)buff, /*same size as the Q: force realloc*/bsz};

/* construct a valid query, though irrelevant for the test a.t.p. */
wmemset(buff, L'*', bsz);
wmemcpy(buff, select.str, select.cnt);
buff[bsz - 2] = L'\'';
buff[bsz - 1] = L'\0';

DBCH(dbc)->pack_json = FALSE;
ASSERT_TRUE(SQL_SUCCEEDED(attach_sql(STMH(stmt), (SQLWCHAR *)buff,
bsz - 1)));
ASSERT_TRUE(SQL_SUCCEEDED(serialize_statement(STMH(stmt), &body)));
ASSERT_NE((void *)body.str, (void *)buff);
free(body.str);
}

TEST_F(Queries, SQLNativeSql) {
#undef SRC_STR
#define SRC_STR "SELECT 1"
Expand Down

0 comments on commit 0c030e3

Please sign in to comment.