From 5f8f8e176c048bf9f817a0bbc5d76b54ce75eff2 Mon Sep 17 00:00:00 2001 From: Tony Cook Date: Wed, 18 Jan 2006 14:06:50 +0000 Subject: [PATCH] - reading a raw image no longer exits on a short read or read error, and returns an appropriate error message in $im->errstr - write failures when writing a raw image now return a useful message in $im->errstr --- Changes | 4 ++++ Imager.pm | 4 ++-- iolayer.c | 12 ++++++++++-- raw.c | 14 +++++++++++++- t/t103raw.t | 29 ++++++++++++++++++++++++++++- 5 files changed, 57 insertions(+), 6 deletions(-) diff --git a/Changes b/Changes index c1bdb15d..ba521670 100644 --- a/Changes +++ b/Changes @@ -1307,6 +1307,10 @@ Revision history for Perl extension Imager. - error messages when writing PNM images were always 'unable to write pnm image', more useful messages are now reported. - convert t/t103raw.t to Test::More +- reading a raw image no longer exits on a short read or read error, + and returns an appropriate error message in $im->errstr +- write failures when writing a raw image now return a useful + message in $im->errstr ================================================================= diff --git a/Imager.pm b/Imager.pm index 244d14cd..01024452 100644 --- a/Imager.pm +++ b/Imager.pm @@ -1311,7 +1311,7 @@ sub read { $params{storechannels}, $params{interleave}); if ( !defined($self->{IMG}) ) { - $self->{ERRSTR}='unable to read raw image'; + $self->{ERRSTR}=$self->_error_as_msg(); return undef; } $self->{DEBUG} && print "loading a raw file\n"; @@ -1468,7 +1468,7 @@ sub write { $self->_set_opts(\%input, "raw_", $self) or return undef; if ( !i_writeraw_wiol($self->{IMG},$IO) ) { - $self->{ERRSTR}='unable to write raw image'; + $self->{ERRSTR} = $self->_error_as_msg(); return undef; } $self->{DEBUG} && print "writing a raw file\n"; diff --git a/iolayer.c b/iolayer.c index 1bbda115..a425bff8 100644 --- a/iolayer.c +++ b/iolayer.c @@ -1064,11 +1064,19 @@ io_slurp(io_glue *ig, unsigned char **c) { =cut */ static ssize_t fd_read(io_glue *ig, void *buf, size_t count) { + ssize_t result; #ifdef _MSC_VER - return _read(ig->source.fdseek.fd, buf, count); + result = _read(ig->source.fdseek.fd, buf, count); #else - return read(ig->source.fdseek.fd, buf, count); + result = read(ig->source.fdseek.fd, buf, count); #endif + + /* 0 is valid - means EOF */ + if (result < 0) { + i_push_errorf(0, "read() failure: %s (%d)", strerror(errno), errno); + } + + return result; } static ssize_t fd_write(io_glue *ig, const void *buf, size_t count) { diff --git a/raw.c b/raw.c index 8b363d5a..ac799ac4 100644 --- a/raw.c +++ b/raw.c @@ -53,6 +53,8 @@ i_img * i_readraw_wiol(io_glue *ig, int x, int y, int datachannels, int storechannels, int intrl) { i_img* im; int rc,k; + + i_clear_error(); unsigned char *inbuffer; unsigned char *ilbuffer; @@ -83,7 +85,17 @@ i_readraw_wiol(io_glue *ig, int x, int y, int datachannels, int storechannels, i k=0; while( kysize ) { rc = ig->readcb(ig, inbuffer, inbuflen); - if (rc != inbuflen) { fprintf(stderr,"Premature end of file.\n"); exit(2); } + if (rc != inbuflen) { + if (rc < 0) + i_push_error(0, "error reading file"); + else + i_push_error(0, "premature end of file"); + i_img_destroy(im); + myfree(inbuffer); + if (intrl != 0) myfree(ilbuffer); + if (datachannels != storechannels) myfree(exbuffer); + return NULL; + } interleave(inbuffer,ilbuffer,im->xsize,datachannels); expandchannels(ilbuffer,exbuffer,im->xsize,datachannels,storechannels); /* FIXME: Do we ever want to save to a virtual image? */ diff --git a/t/t103raw.t b/t/t103raw.t index b565cbf7..94996ff3 100644 --- a/t/t103raw.t +++ b/t/t103raw.t @@ -1,7 +1,7 @@ #!perl -w use strict; use lib 't'; -use Test::More tests => 17; +use Test::More tests => 23; use Imager qw(:all); init_log("testout/t103raw.log",1); @@ -152,6 +152,33 @@ SKIP: } } +{ # error handling checks + # should get an error writing to a open for read file + # make a empty file + open RAW, "> testout/t103_empty.raw" + or die "Cannot create testout/t103_empty.raw: $!"; + close RAW; + open RAW, "< testout/t103_empty.raw" + or die "Cannot open testout/t103_empty.raw: $!"; + my $im = Imager->new(xsize => 50, ysize=>50); + ok(!$im->write(fh => \*RAW, type => 'raw'), + "write to open for read handle"); + cmp_ok($im->errstr, '=~', '^Could not write to file: write\(\) failure', + "check error message"); + close RAW; + + # should get an error reading an empty file + ok(!$im->read(file => 'testout/t103_empty.raw', xsize => 50, ysize=>50, type=>'raw'), + 'read an empty file'); + is($im->errstr, 'premature end of file', "check message"); + open RAW, "> testout/t103_empty.raw" + or die "Cannot create testout/t103_empty.raw: $!"; + ok(!$im->read(fh => \*RAW, , xsize => 50, ysize=>50, type=>'raw'), + 'read a file open for write'); + cmp_ok($im->errstr, '=~', '^error reading file: read\(\) failure', "check message"); + +} + sub read_test { my ($in, $xsize, $ysize, $data, $store, $intrl, $base) = @_; open FH, $in or die "Cannot open $in: $!"; -- 2.30.2