shithub: dumb

Download patch

ref: 14a2e223997ccdcc3da3e9ecd6e8bfb0d879ab7c
parent: e9320b6d48769d669cbd402959a0982867c6319c
author: Simon Naarmann <s.naarmann@gmail.com>
date: Sat Oct 7 10:43:44 EDT 2017

fix internal getnc to return dumb_ssize_t

Return type should match what we declare in dumb.h.
Temporary variable should be long, because A4 returns a long here.
Setting/checking errno to return -1 when we can't read anything,
but returning number of bytes read if we read at least a few bytes.

Consider clarifying the getnc spec what we should return when the caller
wishes for 20 bytes, we can only read 10, then encounter an error. We
should probably return 10 then.

--- a/DUMBFILE_SYSTEM.md
+++ b/DUMBFILE_SYSTEM.md
@@ -147,7 +147,7 @@
 * the number of bytes successfully read, if it was possible to read at least
 one byte.
 
-* `-1` on error.
+* `-1` on error, when it was not possible to read even a single byte.
 
 This function shall bytes from the file `f` and store them in sequence in the
 buffer beginning at `ptr`. It shall read fewer than `n` bytes if end of file
--- a/src/allegro/packfile.c
+++ b/src/allegro/packfile.c
@@ -59,12 +59,15 @@
     return c;
 }
 
-static size_t dumb_packfile_getnc(char *ptr, size_t n, void *f) {
+static dumb_ssize_t dumb_packfile_getnc(char *ptr, size_t n, void *f) {
     dumb_packfile *file = (dumb_packfile *)f;
-    int nr = pack_fread(ptr, n, file->p);
-    if (nr > 0)
+    errno = 0;
+    long nr = pack_fread(ptr, n, file->p);
+    if (nr > 0) {
         file->pos += nr;
-    return nr;
+        return nr;
+    }
+    return errno != 0 ? -1 : 0;
 }
 
 static void dumb_packfile_close(void *f) {