Skip to content

Commit

Permalink
Treat number components that overflow as strings
Browse files Browse the repository at this point in the history
If a number component is too large to be represented by uint32, simply
start treating the component as a string instead of raising RuntimeError.
  • Loading branch information
mislav committed Feb 13, 2015
1 parent a12a8a4 commit 472ce85
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
22 changes: 14 additions & 8 deletions ext/version_sorter/version_sorter.c
Expand Up @@ -112,20 +112,26 @@ parse_version_number(const char *string)

if (isdigit(string[offset])) {
uint32_t number = 0;
uint16_t start = offset;
int overflown = 0;

while (isdigit(string[offset])) {
uint32_t old_number = number;
number = (10 * number) + (string[offset] - '0');

if (number < old_number)
rb_raise(rb_eRuntimeError,
"overflow when comparing numbers in version string");
if (!overflown) {
uint32_t old_number = number;
number = (10 * number) + (string[offset] - '0');
if (number < old_number) overflown = 1;
}

offset++;
}

version->comp[comp_n].number = number;
num_flags |= (1 << comp_n);
if (overflown) {
version->comp[comp_n].string.offset = start;
version->comp[comp_n].string.len = offset - start;
} else {
version->comp[comp_n].number = number;
num_flags |= (1 << comp_n);
}
comp_n++;
continue;
}
Expand Down
13 changes: 13 additions & 0 deletions test/version_sorter_test.rb
Expand Up @@ -30,4 +30,17 @@ def test_reverse_sorts_versions_correctly

assert_equal sorted_versions, VersionSorter.rsort(versions)
end

def test_does_not_raise_on_number_overflow
big_numbers = [
(2**32).to_s,
(2**32 + 1).to_s,
(2**32 + 2).to_s,
(2**32 - 2).to_s,
(2**32 - 1).to_s,
]
randomized = big_numbers.sample(big_numbers.size)

assert_equal big_numbers, VersionSorter.sort(randomized)
end
end

0 comments on commit 472ce85

Please sign in to comment.