Skip to content

Commit df90ddd

Browse files
author
Kevin Baribeau
authored
Fix segfault when from_string is passed nil (#106)
Fix segfault when from_string is passed nil
1 parent e1f41ac commit df90ddd

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
66

77
We track the MAJOR and MINOR version levels of Uber's H3 project (https://github.com/uber/h3) but maintain independent patch levels so we can make small fixes and non breaking changes.
88

9+
## [3.7.3] - Unreleased
10+
### Fixed
11+
- `H3.from_string(nil)` should not crash
12+
913
## [3.7.2] - 2020-07-17
1014
### Fixed
1115
- kRing of invalid indexes should not crash.

lib/h3/bindings/private.rb

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ module Private
5757
attach_function :point_distance_m, :pointDistM, [GeoCoord, GeoCoord], :double
5858
attach_function :polyfill, [GeoPolygon, Resolution, H3IndexesOut], :void
5959
attach_function :res_0_indexes, :getRes0Indexes, [H3IndexesOut], :void
60+
attach_function :string_to_h3, :stringToH3, %i[string], :h3_index
6061
attach_function :uncompact, [H3IndexesIn, :size_t, H3IndexesOut, :size_t, Resolution], :bool
6162
end
6263
end

lib/h3/inspection.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,13 @@ module Inspection
4444
# H3.from_string("8928308280fffff")
4545
# 617700169958293503
4646
#
47+
# @raise [ArgumentError] If h3_string is nil
48+
#
4749
# @return [Integer] H3 index
48-
attach_function :from_string, :stringToH3, %i[string], :h3_index
50+
def from_string(h3_string)
51+
raise ArgumentError if h3_string.nil?
52+
Bindings::Private.string_to_h3(h3_string)
53+
end
4954

5055
# @!method pentagon?(h3_index)
5156
#

spec/inspection_spec.rb

+13-4
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,21 @@
2020
end
2121

2222
describe ".from_string" do
23-
let(:h3_index) { "8928308280fffff"}
24-
let(:result) { h3_index.to_i(16) }
25-
2623
subject(:from_string) { H3.from_string(h3_index) }
24+
context "- valid" do
25+
let(:h3_index) { "8928308280fffff"}
26+
let(:result) { h3_index.to_i(16) }
2727

28-
it { is_expected.to eq(result) }
28+
it { is_expected.to eq(result) }
29+
end
30+
31+
context "- nil" do
32+
let(:h3_index) { nil }
33+
34+
it "raises an error" do
35+
expect { subject }.to raise_error(ArgumentError)
36+
end
37+
end
2938
end
3039

3140
describe ".to_string" do

0 commit comments

Comments
 (0)