Skip to content

Commit

Permalink
Fixes #9 segfaults with empty strings
Browse files Browse the repository at this point in the history
Not the most lovable fix but functional
  • Loading branch information
jamesprior committed Sep 24, 2017
1 parent 8572fc8 commit 6eba093
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
15 changes: 12 additions & 3 deletions ext/pdf417/pdf417.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,20 @@ static VALUE rb_pdf417_lib_encode_text(VALUE self, VALUE text) {
pdf417init(&p);
p.text = StringValuePtr(text);
p.lenText = (int)RSTRING_LEN(text);

if(p.lenText == 0){
return rb_ary_new2(0);
}

fetchCodewords(&p);
if (p.error) {
pdf417free(&p);
return Qnil; //could also return list or raise something
}

list = rb_ary_new2(p.lenCodewords);

pdf417free(&p);

for (k = 0; k < p.lenCodewords; ++k) {
rb_ary_push(list, INT2NUM(p.codewords[k]));
}
Expand Down Expand Up @@ -125,6 +129,11 @@ static VALUE rb_pdf417_lib_codewords(VALUE self) {
pdf417init(&p);
p.text = StringValuePtr(text);
p.lenText = (int)RSTRING_LEN(text);

if(p.lenText == 0){
return rb_ary_new2(0);
}

fetchCodewords(&p);
if (p.error) {
pdf417free(&p);
Expand Down
9 changes: 6 additions & 3 deletions test/pdf417/lib_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
require 'test_helper'

class PDF417::Lib::LibTest < Minitest::Test

should "not fail init with empty text" do
assert PDF417::Lib.new(text: '')
end

should "initialize text" do
b = PDF417::Lib.new("fred")
Expand Down Expand Up @@ -61,9 +65,8 @@ class PDF417::Lib::LibTest < Minitest::Test
refute_equal with_null_bytes, without_null_bytes
end

should "not fail with empty text" do
should "not fail encode_text with empty text" do
assert_equal [], PDF417::Lib.encode_text("")
end

end

end

0 comments on commit 6eba093

Please sign in to comment.