shithub: 9pro

Download patch

ref: f9600150b0e967e3edc95944b0a127f596b1de62
parent: d4afafed9b155b504d29c72755e2987a0724f461
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Sun Dec 22 14:48:56 EST 2019

Squashed 'c9/' changes from 8a22517..d7520a6

d7520a6 make it compile with -Wpedantic
00704e0 better errors

git-subtree-dir: c9
git-subtree-split: d7520a620bfaa6e7f242c069f60c681e7478c6b0

--- a/c9.c
+++ b/c9.c
@@ -5,8 +5,8 @@
  * any sense to copy-paste protocol documentation, which
  * you can read at http://man.cat-v.org/plan_9/5/, see 'intro'.
  */
-#include <string.h>
 #include <stdint.h>
+#include <string.h>
 #include "c9.h"
 
 enum
@@ -138,11 +138,11 @@
 	if(tag != 0xffff){
 		uint32_t d = tag / C9tagsbits, m = tag % C9tagsbits;
 		if(tag >= C9maxtags){
-			c->error("freetag: invalid tag");
+			c->error("freetag: invalid tag %u", (uint32_t)tag);
 			return -1;
 		}
 		if((c->tags[d] & 1<<m) != 0){
-			c->error("freetag: double free");
+			c->error("freetag: double free for tag %u", (uint32_t)tag);
 			return -1;
 		}
 		if(c->lowfreetag > tag)
@@ -158,12 +158,12 @@
 	uint8_t *p = NULL;
 
 	if(size > c->msize-4-1-2){
-		c->error("T: invalid size");
+		c->error("T: invalid size %u", size);
 		*err = C9Esize;
 	}else if((*err = newtag(c, type, tag)) == 0){
 		size += 4+1+2;
 		if((p = c->begin(c, size)) == NULL){
-			c->error("T: no buffer");
+			c->error("T: no buffer for %u bytes", size);
 			freetag(c, *tag);
 			*err = C9Ebuf;
 		}else{
@@ -183,7 +183,7 @@
 	C9error err;
 
 	if(msize < C9minmsize){
-		c->error("c9version: msize too small");
+		c->error("c9version: msize too small: %u", msize);
 		return C9Einit;
 	}
 	memset(c->tags, 0xff, sizeof(c->tags));
@@ -207,7 +207,7 @@
 	C9error err;
 
 	if(ulen > C9maxstr || alen > C9maxstr){
-		c->error("c9auth: string too long");
+		c->error("c9auth: string too long: %u chars", ulen > alen ? ulen : alen);
 		return C9Estr;
 	}
 	if((b = T(c, 4+2+ulen+2+alen, Tauth, tag, &err)) != NULL){
@@ -248,7 +248,7 @@
 	C9error err;
 
 	if(ulen > C9maxstr || alen > C9maxstr){
-		c->error("c9attach: string too long");
+		c->error("c9attach: string too long: %u chars", ulen > alen ? ulen : alen);
 		return C9Estr;
 	}
 	if((b = T(c, 4+4+2+ulen+2+alen, Tattach, tag, &err)) != NULL){
@@ -272,13 +272,13 @@
 	for(sz = i = 0; i < (int)sizeof(len)/sizeof(len[0]) && path[i] != NULL; i++){
 		len[i] = safestrlen(path[i]);
 		if(len[i] == 0 || len[i] > C9maxstr){
-			c->error("c9walk: path element too long");
+			c->error("c9walk: invalid path element: %u chars", len[i]);
 			return C9Epath;
 		}
 		sz += 2 + len[i];
 	}
 	if(path[i] != NULL || i == 0){
-		c->error("c9walk: invalid elements !(0 < %d <= %d)", i, C9maxpathel);
+		c->error("c9walk: invalid elements !(0 < %u <= %u)", i, C9maxpathel);
 		return C9Epath;
 	}
 
@@ -315,7 +315,7 @@
 	C9error err;
 
 	if(nlen == 0 || nlen > C9maxstr){
-		c->error("c9create: invalid name");
+		c->error("c9create: invalid name: %u chars", nlen);
 		return C9Epath;
 	}
 	if((b = T(c, 4+2+nlen+4+1, Tcreate, tag, &err)) != NULL){
@@ -413,11 +413,11 @@
 	C9error err;
 
 	if(nlen == 0 || nlen > C9maxstr){
-		c->error("c9wstat: invalid name");
+		c->error("c9wstat: invalid name: %u chars", nlen);
 		return C9Epath;
 	}
 	if(ulen > C9maxstr || glen > C9maxstr){
-		c->error("c9wstat: string too long");
+		c->error("c9wstat: string too long: %u chars", ulen > glen ? ulen : glen);
 		return C9Estr;
 	}
 	if((b = T(c, 4+2+2+statsz, Twstat, tag, &err)) != NULL){
@@ -471,7 +471,7 @@
 	r.tag = r16(&b);
 	if(r.type != Rversion){
 		if(r.tag >= C9maxtags){
-			c->error("c9proc: invalid tag 0x%x", r.tag);
+			c->error("c9proc: invalid tag %u", (uint32_t)r.tag);
 			return C9Epkt;
 		}
 		if(freetag(c, r.tag) != 0)
@@ -500,7 +500,7 @@
 		if(sz < 2+13 || (cnt = r16(&b))*13 > sz-2)
 			goto error;
 		if(cnt > C9maxpathel){
-			c->error("c9proc: Rwalk !(%d <= %d)", cnt, C9maxpathel);
+			c->error("c9proc: Rwalk !(%u <= %u)", cnt, C9maxpathel);
 			return C9Epath;
 		}
 		for(i = 0; i < cnt; i++){
@@ -514,10 +514,8 @@
 
 	case Rstat:
 		b += 2; sz -= 2;
-		if((err = c9parsedir(c, &r.stat, &b, &sz)) != 0){
-			c->error("c9proc");
+		if((err = c9parsedir(c, &r.stat, &b, &sz)) != 0)
 			return err;
-		}
 		r.numqid = 1;
 		c->r(c, &r);
 		break;
@@ -585,7 +583,7 @@
 	}
 	return 0;
 error:
-	c->error("c9proc: invalid packet (type=%d)", r.type);
+	c->error("c9proc: invalid packet type %u", r.type);
 	return C9Epkt;
 }
 
@@ -597,6 +595,7 @@
 	uint8_t *b;
 	uint32_t cnt, sz;
 
+	sz = 0;
 	if(*size < 49 || (sz = r16(t)) < 47 || *size < 2+sz)
 		goto error;
 	*size -= 2+sz;
@@ -624,7 +623,7 @@
 	*t += sz;
 	return 0;
 error:
-	c->error("c9parsedir: invalid size");
+	c->error("c9parsedir: invalid size: size=%u sz=%u", *size, sz);
 	return C9Epkt;
 }
 
@@ -636,12 +635,12 @@
 	uint8_t *p = NULL;
 
 	if(size > c->msize-4-1-2){
-		c->error("R: invalid size");
+		c->error("R: invalid size %u", size);
 		*err = C9Esize;
 	}else{
 		size += 4+1+2;
 		if((p = c->begin(c, size)) == NULL){
-			c->error("R: no buffer");
+			c->error("R: no buffer for %u bytes", size);
 			*err = C9Ebuf;
 		}else{
 			*err = 0;
@@ -690,7 +689,7 @@
 	C9error err;
 
 	if(len > C9maxstr){
-		c->error("s9error: invalid ename");
+		c->error("s9error: invalid ename: %u chars", len);
 		return C9Estr;
 	}
 	if((b = R(c, 2+len, Rerror, tag, &err)) != NULL){
@@ -735,7 +734,7 @@
 
 	for(n = 0; n < C9maxpathel && qids[n] != NULL; n++);
 	if(n > C9maxpathel){
-		c->error("s9walk: invalid elements !(0 <= %d <= %d)", n, C9maxpathel);
+		c->error("s9walk: invalid elements !(0 <= %u <= %u)", n, C9maxpathel);
 		return C9Epath;
 	}
 
@@ -831,11 +830,13 @@
 		mulen = safestrlen(s->muid);
 
 		if(nlen == 0 || nlen > C9maxstr){
-			c->error("s9readdir: invalid name");
+			c->error("s9readdir: invalid name: %u chars", nlen);
 			return C9Epath;
 		}
 		if(ulen > C9maxstr || glen > C9maxstr || mulen > C9maxstr){
-			c->error("s9readdir: string too long");
+			ulen = ulen > glen ? ulen : glen;
+			ulen = ulen > mulen ? ulen : mulen;
+			c->error("s9readdir: string too long: %u chars", ulen);
 			return C9Estr;
 		}
 
@@ -908,11 +909,13 @@
 	C9error err;
 
 	if(nlen == 0 || nlen > C9maxstr){
-		c->error("s9stat: invalid name");
+		c->error("s9stat: invalid name: %u chars", nlen);
 		return C9Epath;
 	}
 	if(ulen > C9maxstr || glen > C9maxstr || mulen > C9maxstr){
-		c->error("s9stat: string too long");
+		ulen = ulen > glen ? ulen : glen;
+		ulen = ulen > mulen ? ulen : mulen;
+		c->error("s9stat: string too long: %u chars", ulen);
 		return C9Estr;
 	}
 
@@ -982,7 +985,7 @@
 	sz -= 3;
 
 	if((c->svflags & Svver) == 0 && t.type != Tversion){
-		c->error("s9proc: expected Tversion, got %d", t.type);
+		c->error("s9proc: expected Tversion, got %u", t.type);
 		return C9Epkt;
 	}
 
@@ -1026,7 +1029,7 @@
 		t.fid = r32(&b);
 		t.walk.newfid = r32(&b);
 		if((n = r16(&b)) > 16){
-			c->error("s9proc: Twalk !(%d <= 16)", n);
+			c->error("s9proc: Twalk !(%u <= 16)", n);
 			return C9Epath;
 		}
 		sz -= 4+4+2;
@@ -1035,7 +1038,7 @@
 				if(sz < 2 || (cnt = r16(&b)) > sz-2)
 					goto error;
 				if(cnt < 1){
-					c->error("s9proc: Twalk invalid element [%d]", i);
+					c->error("s9proc: Twalk invalid element [%u]", i);
 					return C9Epath;
 				}
 				b[-2] = 0;
@@ -1160,7 +1163,7 @@
 	}
 	return 0;
 error:
-	c->error("s9proc: invalid packet (type=%d)", t.type);
+	c->error("s9proc: invalid packet (type=%u)", t.type);
 	return C9Epkt;
 }
 
--- a/c9.h
+++ b/c9.h
@@ -5,11 +5,6 @@
 typedef struct C9stat C9stat;
 typedef struct C9ctx C9ctx;
 typedef struct C9qid C9qid;
-typedef enum C9error C9error;
-typedef enum C9mode C9mode;
-typedef enum C9rtype C9rtype;
-typedef enum C9ttype C9ttype;
-typedef enum C9qt C9qt;
 typedef uint32_t C9fid;
 typedef uint16_t C9tag;
 
@@ -20,7 +15,7 @@
 #define C9nofid ((C9fid)~0)
 
 /* C9modes for opening a file. */
-enum C9mode
+typedef enum
 {
 	C9read = 0,
 	C9write = 1,
@@ -28,9 +23,9 @@
 	C9exec = 3,
 	C9trunc = 0x10,
 	C9rclose = 0x40,
-};
+}C9mode;
 
-enum C9perm
+typedef enum
 {
 	/* User/owner. */
 	C9permur = 1<<8, /* Readable. */
@@ -46,7 +41,7 @@
 	C9permor = 1<<2,
 	C9permow = 1<<1,
 	C9permox = 1<<0,
-};
+}C9perm;
 
 /* Directory. */
 #define C9permdir 0x80000000
@@ -68,7 +63,7 @@
 };
 
 /* Errors. */
-enum C9error
+typedef enum
 {
 	C9Einit = -1,  /* Initialization failed. */
 	C9Ever = -2,   /* Protocol version doesn't match. */
@@ -79,10 +74,10 @@
 	C9Eflush = -7, /* Limit of outstanding flushes reached. */
 	C9Esize = -8,  /* Can't fit data in one message. */
 	C9Estr = -9    /* Bad string. */
-};
+}C9error;
 
 /* Request types. */
-enum C9ttype
+typedef enum
 {
 	Tversion = 100,
 	Tauth = 102,
@@ -97,10 +92,10 @@
 	Tremove = 122,
 	Tstat = 124,
 	Twstat = 126
-};
+}C9ttype;
 
 /* Response types. */
-enum C9rtype
+typedef enum
 {
 	Rversion = 101,
 	Rauth = 103,
@@ -116,10 +111,10 @@
 	Rremove = 123,
 	Rstat = 125,
 	Rwstat = 127
-};
+}C9rtype;
 
 /* Unique file id type. */
-enum C9qt
+typedef enum
 {
 	C9qtdir = 1<<7,
 	C9qtappend = 1<<6,
@@ -127,7 +122,7 @@
 	C9qtauth = 1<<3,
 	C9qttmp = 1<<2,
 	C9qtfile = 0
-};
+}C9qt;
 
 /* Unique file id. */
 struct C9qid
@@ -290,7 +285,7 @@
 	void (*t)(C9ctx *ctx, C9t *t) __attribute__((nonnull(1, 2)));
 
 	/* Callback for error messages. */
-	void (*error)(const char *fmt, ...) __attribute__((nonnull(1)));
+	void (*error)(const char *fmt, ...) __attribute__((nonnull(1), format(printf, 1, 2)));
 
 	/* Auxiliary data, can be used by any of above callbacks. */
 	struct C9aux *aux;