]> git.draconx.ca Git - liblbx.git/commitdiff
liblbx: Correct EOF handling in lbx_file_read.
authorNick Bowler <nbowler@draconx.ca>
Fri, 31 Jan 2014 05:21:04 +0000 (00:21 -0500)
committerNick Bowler <nbowler@draconx.ca>
Fri, 31 Jan 2014 05:43:07 +0000 (00:43 -0500)
We should never see an EOF from the underlying I/O when reading archive
members, because we setup the read request to only return bytes that are
supposed to be there.

So if EOF happens, flag this as an error and do *not* set the eof flag
in the state structure.  Also fixup lbxtool to properly handle this.

src/lbx.c
src/lbxtool.c
tests/broken-archives.tap

index 1198ebbc2c0b13e08aa4cbb7c914ae431d4eb48c..f96344d2ce7cfc717796531f244d789414973021 100644 (file)
--- a/src/lbx.c
+++ b/src/lbx.c
@@ -262,8 +262,13 @@ size_t lbx_file_read(struct lbx_file_state *f, void *buf, size_t n)
        rc = fops->read(buf, want, f->lbx->f);
        f->offset += rc;
 
-       if (want < n || (rc < want && fops->eof(f->lbx->f)))
+       if (rc < want) {
+               if (fops->eof(f->lbx->f))
+                       lbx_error_raise(LBX_EEOF);
+       } else if (rc < n) {
                f->eof = 1;
+       }
+
        return rc;
 }
 
index 767c30925b26c5d564920dbc154107a36a9066dd..3c4e436450caf0db09f6fe9f79cbbd653dd0add2 100644 (file)
@@ -110,19 +110,19 @@ int extract_file(LBXfile *f, const struct lbx_statbuf *stat)
                unsigned char buf[1024];
 
                rc = lbx_file_read(f, buf, sizeof buf);
-               if (rc == 0) {
-                       if (lbx_file_eof(f))
-                               ret = 0;
-                       break;
-               }
-
-               if (fwrite(buf, rc, 1, of) != 1) {
-                       tool_err(0, "%s: fwrite", stat->name);
-                       break;
+               if (rc > 0) {
+                       /* Write out any data we got. */
+                       if (fwrite(buf, rc, 1, of) != 1) {
+                               tool_err(0, "%s: fwrite", stat->name);
+                               break;
+                       }
                }
 
+               /* Now test for read errors */
                if (rc < sizeof buf) {
-                       if (lbx_file_eof(f))
+                       if (!lbx_file_eof(f))
+                               tool_err(-1, "error reading archive: %s", lbx_errmsg());
+                       else
                                ret = 0;
                        break;
                }
index 3997ca3548af37e5f4b5d0943a68926013610f65..2eacc7f36d39bac8084056d7360c7071aab16486 100644 (file)
@@ -24,4 +24,4 @@ dx_create_testdir
 
 failed=false
 $LBXTOOL -xf "$testdata/arch-trunc.lbx" || failed=true
-command_ok_ "EOF handled in archive" -D TODO $failed
+command_ok_ "EOF handled in archive" $failed