From 99ea8fdb7ba911aba7f259ee8f235b1948fb5da6 Mon Sep 17 00:00:00 2001 From: Alexey Romanov Date: Fri, 14 Sep 2012 16:11:49 +0400 Subject: [PATCH] Fixed issue #13 (binding ints from 128 to 255 incorrectly) --- c_src/sqlite3_drv.c | 4 ++-- test/sqlite3_test.erl | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/c_src/sqlite3_drv.c b/c_src/sqlite3_drv.c index a7f4a34..309d67c 100644 --- a/c_src/sqlite3_drv.c +++ b/c_src/sqlite3_drv.c @@ -357,7 +357,7 @@ static inline int decode_and_bind_param( // include space for null separator char_buf_val = driver_alloc((*p_size + 1) * sizeof(char)); ei_decode_string(buffer, p_index, char_buf_val); - result = sqlite3_bind_text(statement, param_index, char_buf_val, *p_size, &driver_free_fun); + result = sqlite3_bind_text(statement, param_index, char_buf_val, *p_size, &driver_free_fun); break; case ERL_BINARY_EXT: char_buf_val = driver_alloc(*p_size * sizeof(char)); @@ -413,7 +413,7 @@ static int bind_parameters( acc_string = driver_alloc(sizeof(char*) * (*p_size + 1)); ei_decode_string(buffer, p_index, acc_string); for (param_index = 1; param_index <= *p_size; param_index++) { - sqlite3_bind_int(statement, param_index, (int) acc_string[param_index - 1]); + sqlite3_bind_int(statement, param_index, (int) (unsigned char) acc_string[param_index - 1]); } driver_free(acc_string); return 0; diff --git a/test/sqlite3_test.erl b/test/sqlite3_test.erl index 055ef67..f35297c 100644 --- a/test/sqlite3_test.erl +++ b/test/sqlite3_test.erl @@ -50,7 +50,8 @@ all_test_() -> ?FuncTest(large_number), ?FuncTest(unicode), ?FuncTest(acc_string_encoding), - ?FuncTest(large_offset)]}. + ?FuncTest(large_offset), + ?FuncTest(issue13)]}. open_db() -> sqlite3:open(ct, [in_memory]). @@ -310,6 +311,21 @@ large_offset() -> [{columns, ["id"]}, {rows, []}, {error, 20, _}], sqlite3:sql_exec(ct, "select * from large_offset limit 1 offset 9223372036854775808")). +issue13() -> + drop_table_if_exists(ct, issue13), + ok = sqlite3:create_table(ct, issue13, [{foo, integer}]), + sqlite3:write_many(ct, issue13, + [[{foo, X}] || X <- [-1, 0, 127, 128, 255, 256]]), + ?assertEqual( + [{columns, ["foo"]}, {rows, [{255}, {256}]}], + sqlite3:sql_exec(ct, "select foo from issue13 where foo > 128;")), + ?assertEqual( + [{columns, ["foo"]}, {rows, [{255}, {256}]}], + sqlite3:sql_exec(ct, "select foo from issue13 where foo > ?;", [128.0])), + ?assertEqual( + [{columns, ["foo"]}, {rows, [{255}, {256}]}], + sqlite3:sql_exec(ct, "select foo from issue13 where foo > ?;", [128])). + % create, read, update, delete %%==================================================================== %% Internal functions