Skip to content

Commit

Permalink
fix: handle zero denominator
Browse files Browse the repository at this point in the history
Returns Float::NAN when 0/0 and Float::INFINITY when N/0

close #12
  • Loading branch information
tonytonyjan committed Oct 2, 2017
1 parent 2f245ac commit 0a0ca2e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
28 changes: 24 additions & 4 deletions ext/exif/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ static VALUE brackets(VALUE self, VALUE ifd_symbol);
static void each_content(ExifContent *ec, void *user_data);
static void each_entry(ExifEntry *, void *user_data);
static VALUE exif_entry_to_rb_value(ExifEntry *);
static VALUE rational_to_num(ExifRational rational);
static VALUE srational_to_num(ExifSRational srational);

void init_data(){
int length;
Expand Down Expand Up @@ -168,23 +170,23 @@ static VALUE exif_entry_to_rb_value(ExifEntry *entry){
ret = rb_ary_new2(entry->components);
for(i = 0; i < entry->components; i++){
rational = exif_get_rational(entry->data + i * size, order);
rb_ary_push(ret, rb_rational_new(ULONG2NUM(rational.numerator), ULONG2NUM(rational.denominator)));
rb_ary_push(ret, rational_to_num(rational));
}
} else {
rational = exif_get_rational(entry->data, order);
ret = rb_rational_new(ULONG2NUM(rational.numerator), ULONG2NUM(rational.denominator));
ret = rational_to_num(rational);
}
break;
case EXIF_FORMAT_SRATIONAL:
if(entry->components > 1){
ret = rb_ary_new2(entry->components);
for(int i = 0; i < entry->components; i++){
srational = exif_get_srational(entry->data + i * size, order);
rb_ary_push(ret, rb_rational_new(LONG2FIX(srational.numerator), LONG2FIX(srational.denominator)));
rb_ary_push(ret, srational_to_num(srational));
}
} else {
srational = exif_get_srational(entry->data, order);
ret = rb_rational_new(LONG2FIX(srational.numerator), LONG2FIX(srational.denominator));
ret = srational_to_num(srational);
}
break;
case EXIF_FORMAT_DOUBLE:
Expand All @@ -195,3 +197,21 @@ static VALUE exif_entry_to_rb_value(ExifEntry *entry){

return ret;
}

VALUE rational_to_num(ExifRational rational){
if(rational.numerator == 0 && rational.denominator == 0)
return DBL2NUM(NAN);
else if(rational.denominator == 0)
return DBL2NUM(INFINITY);
else
return rb_rational_new(ULONG2NUM(rational.numerator), ULONG2NUM(rational.denominator));
}

VALUE srational_to_num(ExifSRational srational){
if(srational.numerator == 0 && srational.denominator == 0)
return DBL2NUM(NAN);
else if(srational.denominator == 0)
return DBL2NUM(INFINITY);
else
return rb_rational_new(LONG2NUM(srational.numerator), LONG2NUM(srational.denominator));
}
Binary file added test/infinity.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/nan.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions test/test_exif.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,14 @@ def test_type_error
Exif::Data.new(123)
end
end

def test_nan
data = Exif::Data.new(IO.read(File.expand_path('../nan.jpg', __FILE__)))
assert data.exposure_index.nan?
end

def test_infinity
data = Exif::Data.new(IO.read(File.expand_path('../infinity.jpg', __FILE__)))
assert_equal Float::INFINITY, data.exposure_index
end
end

0 comments on commit 0a0ca2e

Please sign in to comment.