--- /n/sources/plan9/sys/src/cmd/unix/u9fs/fcallconv.c Fri Jan 25 10:50:05 2013 +++ /sys/src/cmd/unix/u9fs/fcallconv.c Thu Jun 25 00:00:00 2026 @@ -4,8 +4,8 @@ extern int old9p; -static uint dumpsome(char*, char*, long); -static void fdirconv(char*, Dir*); +static uint dumpsome(char*, char*, char*, long); +static void fdirconv(char*, char*, Dir*); static char *qidtype(char*, uchar); #define QIDFMT "(%.16llux %lud %s)" @@ -14,133 +14,133 @@ int fcallconv(va_list *arg, Fconv *f1) { Fcall *f; - int fid, type, tag, n, i; + int fid, type, tag, i; char buf[512], tmp[200]; - Dir *d; + char *p, *e; + Dir d; Qid *q; + e = buf+sizeof(buf); f = va_arg(*arg, Fcall*); type = f->type; fid = f->fid; tag = f->tag; switch(type){ case Tversion: /* 100 */ - sprint(buf, "Tversion tag %ud msize %ud version '%s'", tag, f->msize, f->version); + seprint(buf, e, "Tversion tag %ud msize %ud version '%s'", tag, f->msize, f->version); break; case Rversion: - sprint(buf, "Rversion tag %ud msize %ud version '%s'", tag, f->msize, f->version); + seprint(buf, e, "Rversion tag %ud msize %ud version '%s'", tag, f->msize, f->version); break; case Tauth: /* 102 */ - sprint(buf, "Tauth tag %ud afid %d uname %s aname %s", tag, + seprint(buf, e, "Tauth tag %ud afid %d uname %s aname %s", tag, f->afid, f->uname, f->aname); break; case Rauth: - sprint(buf, "Rauth tag %ud qid " QIDFMT, tag, + seprint(buf, e, "Rauth tag %ud qid " QIDFMT, tag, f->aqid.path, f->aqid.vers, qidtype(tmp, f->aqid.type)); break; case Tattach: /* 104 */ - sprint(buf, "Tattach tag %ud fid %d afid %d uname %s aname %s", tag, + seprint(buf, e, "Tattach tag %ud fid %d afid %d uname %s aname %s", tag, fid, f->afid, f->uname, f->aname); break; case Rattach: - sprint(buf, "Rattach tag %ud qid " QIDFMT, tag, + seprint(buf, e, "Rattach tag %ud qid " QIDFMT, tag, f->qid.path, f->qid.vers, qidtype(tmp, f->qid.type)); break; case Rerror: /* 107; 106 (Terror) illegal */ - sprint(buf, "Rerror tag %ud ename %s", tag, f->ename); + seprint(buf, e, "Rerror tag %ud ename %s", tag, f->ename); break; case Tflush: /* 108 */ - sprint(buf, "Tflush tag %ud oldtag %ud", tag, f->oldtag); + seprint(buf, e, "Tflush tag %ud oldtag %ud", tag, f->oldtag); break; case Rflush: - sprint(buf, "Rflush tag %ud", tag); + seprint(buf, e, "Rflush tag %ud", tag); break; case Twalk: /* 110 */ - n = sprint(buf, "Twalk tag %ud fid %d newfid %d nwname %d ", tag, fid, f->newfid, f->nwname); + p = seprint(buf, e, "Twalk tag %ud fid %d newfid %d nwname %d ", tag, fid, f->newfid, f->nwname); + if(f->nwname <= MAXWELEM) for(i=0; inwname; i++) - n += sprint(buf+n, "%d:%s ", i, f->wname[i]); + p = seprint(p, e, "%d:%s ", i, f->wname[i]); break; case Rwalk: - n = sprint(buf, "Rwalk tag %ud nwqid %ud ", tag, f->nwqid); - for(i=0; inwqid; i++){ - q = &f->wqid[i]; - n += sprint(buf+n, "%d:" QIDFMT " ", i, - q->path, q->vers, qidtype(tmp, q->type)); - } + p = seprint(buf, e, "Rwalk tag %ud nwqid %ud ", tag, f->nwqid); + if(f->nwqid <= MAXWELEM) + for(i=0; inwqid; i++){ + q = &f->wqid[i]; + p = seprint(p, e, "%d:" QIDFMT " ", i, + q->path, q->vers, qidtype(tmp, q->type)); + } break; case Topen: /* 112 */ - sprint(buf, "Topen tag %ud fid %ud mode %d", tag, fid, f->mode); + seprint(buf, e, "Topen tag %ud fid %ud mode %d", tag, fid, f->mode); break; case Ropen: - sprint(buf, "Ropen tag %ud qid " QIDFMT " iounit %ud ", tag, + seprint(buf, e, "Ropen tag %ud qid " QIDFMT " iounit %ud ", tag, f->qid.path, f->qid.vers, qidtype(tmp, f->qid.type), f->iounit); break; case Tcreate: /* 114 */ - sprint(buf, "Tcreate tag %ud fid %ud name %s perm %M mode %d", + seprint(buf, e, "Tcreate tag %ud fid %ud name %s perm %M mode %d", tag, fid, f->name, (ulong)f->perm, f->mode); break; case Rcreate: - sprint(buf, "Rcreate tag %ud qid " QIDFMT " iounit %ud ", tag, + seprint(buf, e, "Rcreate tag %ud qid " QIDFMT " iounit %ud ", tag, f->qid.path, f->qid.vers, qidtype(tmp, f->qid.type), f->iounit); break; case Tread: /* 116 */ - sprint(buf, "Tread tag %ud fid %d offset %lld count %ud", + seprint(buf, e, "Tread tag %ud fid %d offset %lld count %ud", tag, fid, f->offset, f->count); break; case Rread: - n = sprint(buf, "Rread tag %ud count %ud ", tag, f->count); - dumpsome(buf+n, f->data, f->count); + p = seprint(buf, e, "Rread tag %ud count %ud ", tag, f->count); + dumpsome(p, e, f->data, f->count); break; case Twrite: /* 118 */ - n = sprint(buf, "Twrite tag %ud fid %d offset %lld count %ud ", + p = seprint(buf, e, "Twrite tag %ud fid %d offset %lld count %ud ", tag, fid, f->offset, f->count); - dumpsome(buf+n, f->data, f->count); + dumpsome(p, e, f->data, f->count); break; case Rwrite: - sprint(buf, "Rwrite tag %ud count %ud", tag, f->count); + seprint(buf, e, "Rwrite tag %ud count %ud", tag, f->count); break; case Tclunk: /* 120 */ - sprint(buf, "Tclunk tag %ud fid %ud", tag, fid); + seprint(buf, e, "Tclunk tag %ud fid %ud", tag, fid); break; case Rclunk: - sprint(buf, "Rclunk tag %ud", tag); + seprint(buf, e, "Rclunk tag %ud", tag); break; case Tremove: /* 122 */ - sprint(buf, "Tremove tag %ud fid %ud", tag, fid); + seprint(buf, e, "Tremove tag %ud fid %ud", tag, fid); break; case Rremove: - sprint(buf, "Rremove tag %ud", tag); + seprint(buf, e, "Rremove tag %ud", tag); break; case Tstat: /* 124 */ - sprint(buf, "Tstat tag %ud fid %ud", tag, fid); + seprint(buf, e, "Tstat tag %ud fid %ud", tag, fid); break; case Rstat: - n = sprint(buf, "Rstat tag %ud ", tag); - if(f->nstat > sizeof tmp) - sprint(buf+n, " stat(%d bytes)", f->nstat); + p = seprint(buf, e, "Rstat tag %ud ", tag); + if(f->nstat > sizeof tmp || (old9p?convM2Dold:convM2D)(f->stat, f->nstat, &d, tmp) == 0) + seprint(p, e, " stat(%d bytes)", f->nstat); else{ - d = (Dir*)tmp; - (old9p?convM2Dold:convM2D)(f->stat, f->nstat, d, (char*)(d+1)); - sprint(buf+n, " stat "); - fdirconv(buf+n+6, d); + seprint(p, e, " stat "); + fdirconv(p+6, e, &d); } break; case Twstat: /* 126 */ - n = sprint(buf, "Twstat tag %ud fid %ud", tag, fid); - if(f->nstat > sizeof tmp) - sprint(buf+n, " stat(%d bytes)", f->nstat); + p = seprint(buf, e, "Twstat tag %ud fid %ud", tag, fid); + if(f->nstat > sizeof tmp || (old9p?convM2Dold:convM2D)(f->stat, f->nstat, &d, tmp) == 0) + seprint(p, e, " stat(%d bytes)", f->nstat); else{ - d = (Dir*)tmp; - (old9p?convM2Dold:convM2D)(f->stat, f->nstat, d, (char*)(d+1)); - sprint(buf+n, " stat "); - fdirconv(buf+n+6, d); + seprint(p, e, " stat "); + fdirconv(p+6, e, &d); } break; case Rwstat: - sprint(buf, "Rwstat tag %ud", tag); + seprint(buf, e, "Rwstat tag %ud", tag); break; default: - sprint(buf, "unknown type %d", type); + seprint(buf, e, "unknown type %d", type); } strconv(buf, f1); return(sizeof(Fcall*)); @@ -171,17 +171,17 @@ dirconv(va_list *arg, Fconv *f) { char buf[160]; - fdirconv(buf, va_arg(*arg, Dir*)); + fdirconv(buf, buf+sizeof buf, va_arg(*arg, Dir*)); strconv(buf, f); return(sizeof(Dir*)); } static void -fdirconv(char *buf, Dir *d) +fdirconv(char *buf, char *e, Dir *d) { char tmp[16]; - sprint(buf, "'%s' '%s' '%s' '%s' " + seprint(buf, e, "'%s' '%s' '%s' '%s' " "q " QIDFMT " m %#luo " "at %ld mt %ld l %lld " "t %d d %d", @@ -199,11 +199,15 @@ fdirconv(char *buf, Dir *d) #define DUMPL 64 static uint -dumpsome(char *ans, char *buf, long count) +dumpsome(char *ans, char *e, char *buf, long count) { int i, printable; char *p; + if(buf == nil){ + seprint(ans, e, ""); + return strlen(ans); + } printable = 1; if(count > DUMPL) count = DUMPL; @@ -213,9 +217,13 @@ dumpsome(char *ans, char *buf, long coun p = ans; *p++ = '\''; if(printable){ + if(count > e-p-2) + count = e-p-2; memmove(p, buf, count); p += count; }else{ + if(2*count > e-p-2) + count = (e-p-2)/2; for(i=0; i0 && i%4==0) *p++ = ' ';