Permalink
Browse files

- 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
  • Loading branch information...
Tony Cook
Tony Cook committed Jan 18, 2006
1 parent 0bfa6bd commit 5f8f8e176c048bf9f817a0bbc5d76b54ce75eff2
Showing with 57 additions and 6 deletions.
  1. +4 −0 Changes
  2. +2 −2 Imager.pm
  3. +10 −2 iolayer.c
  4. +13 −1 raw.c
  5. +28 −1 t/t103raw.t
View
@@ -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
=================================================================
View
@@ -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";
View
@@ -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) {
View
14 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( k<im->ysize ) {
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? */
View
@@ -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: $!";

0 comments on commit 5f8f8e1

Please sign in to comment.