Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds changes for correctly setting stream position when error occurs while reading binary int64 #244

Merged
merged 4 commits into from
May 24, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions ionc/ion_reader_binary.c
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ iERR _ion_reader_binary_read_int64(ION_READER *preader, int64_t *p_value)
{
iENTER;
ION_BINARY_READER *binary;
int tid, len;
int tid, len, i;
desaikd marked this conversation as resolved.
Show resolved Hide resolved
uint64_t unsignedInt64 = 0;
BOOL is_negative;
BOOL is_null;
Expand Down Expand Up @@ -1006,7 +1006,17 @@ iERR _ion_reader_binary_read_int64(ION_READER *preader, int64_t *p_value)
IONCHECK(ion_binary_read_uint_64(preader->istream, len, &unsignedInt64));

is_negative = (tid == TID_NEG_INT)? TRUE: FALSE;
IONCHECK(cast_to_int64(unsignedInt64, is_negative, p_value));
iERR result = cast_to_int64(unsignedInt64, is_negative, p_value);

// if an error is thrown reset reader stream's current pointer to the beginning of current value again
if(result) {
desaikd marked this conversation as resolved.
Show resolved Hide resolved
i = len;
while(i--) {
preader->istream->_curr--;
desaikd marked this conversation as resolved.
Show resolved Hide resolved
}
FAILWITH(result);
}
desaikd marked this conversation as resolved.
Show resolved Hide resolved

if (is_negative && *p_value == 0) {
FAILWITH(IERR_INVALID_BINARY);
}
Expand Down
32 changes: 32 additions & 0 deletions test/test_ion_binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,38 @@ TEST(IonBinaryInt, ReaderRejectsNegativeZeroMixedIntTwoByte) {
test_ion_binary_write_from_reader_rejects_negative_zero_int((BYTE *)"\xE0\x01\x00\xEA\x31\x00", 6);
}

void test_ion_binary_reader_threshold_for_int64(BYTE *data, size_t len, char *actual_value) {
hREADER reader;
ION_TYPE type;
int64_t value;
ION_INT big_int_expected;
SIZE str_len, written;

ION_ASSERT_OK(ion_reader_open_buffer(&reader, data, len, NULL));
ION_ASSERT_OK(ion_reader_next(reader, &type));
ASSERT_EQ(tid_INT, type);

// reading value as int64 will throw numeric overflow error
ASSERT_EQ(IERR_NUMERIC_OVERFLOW,ion_reader_read_int64(reader, &value));

// initialize ION_INT and read it as big integer
ION_ASSERT_OK(ion_int_init(&big_int_expected, NULL));
ION_ASSERT_OK(ion_reader_read_ion_int(reader, &big_int_expected));

// convert big integer to string for comparison
ion_int_char_length(&big_int_expected, &str_len);
char *int_str = (char *)malloc(str_len * sizeof(char));
ion_int_to_char(&big_int_expected, (BYTE *)int_str, str_len, &written);

// compare string representation of the value
ASSERT_STREQ(actual_value, int_str);
}

TEST(IonBinaryReader, ReaderThresholdForInt64) {
test_ion_binary_reader_threshold_for_int64((BYTE *)"\xE0\x01\x00\xEA\x28\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 13, "18446744073709551615");
test_ion_binary_reader_threshold_for_int64((BYTE *)"\xE0\x01\x00\xEA\x39\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 14, "-18446744073709551615");
}
desaikd marked this conversation as resolved.
Show resolved Hide resolved

void test_ion_binary_reader_requires_timestamp_fraction_less_than_one(BYTE *data, size_t len) {
hREADER reader;
ION_TYPE type;
Expand Down