Re: [Lit.] Buffer overruns
From: David Wagner (daw_at_taverner.cs.berkeley.edu)
Date: 01/26/05
- Next message: infobahn: "Re: [Lit.] Buffer overruns"
- Previous message: Tim Smith: "Re: Reality check, surrogate factoring"
- In reply to: infobahn: "Re: [Lit.] Buffer overruns"
- Next in thread: infobahn: "Re: [Lit.] Buffer overruns"
- Reply: infobahn: "Re: [Lit.] Buffer overruns"
- Reply: CuriousCat: "Re: [Lit.] Buffer overruns"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Date: Wed, 26 Jan 2005 07:02:48 +0000 (UTC)
infobahn wrote:
>I certainly hope so, because what I /think/ he's suggesting is a
>really bad idea. I /hope/ he's only suggesting a new datatype,
>perhaps buf_t, and a raft of new functions such as bufcpy, bufcmp,
>etc. (No, I *don't* think that's a good idea and I am *not*
>advocating it!)
The suggestion came from Doug Gwyn. I was merely seconding (my
understanding of) his suggestion. I think he was suggesting something
like:
typedef struct {
unsigned char *p;
size_t size; // count of unsigned char allocated for p
} buf_t;
buf_t mkzerobuf(size_t size) {
buf_t b = {(unsigned char *)malloc(size), size};
memset(b.p, 0, size);
return b;
}
buf_t mkstringbuf(char *s) {
size_t size = strlen(s);
buf_t b = mkzerobuf(size+1);
memcpy(b.p, s, size+1);
}
buf_t bufensure(buf_t b, size_t minsize) {
if (b.size < minsize) {
b.p = (unsigned char *)realloc(b.p, minsize);
b.size = minsize;
}
return b;
}
// precondition: dst.p must not alias src.p
buf_t bufcpy(buf_t dst, buf_t src) {
dst = bufensure(dst, src.size);
memcpy(dst.p, src.p, src.size);
return dst;
}
buf_t bufget(buf_t b, size_t i) {
if (i >= b.size)
abort();
return b.p[i];
}
void bufset(buf_t b, size_t i, unsigned char c) {
if (i >= b.size)
abort();
b.p[i] = c;
}
and so on. Keep in mind that this is only my interpretation of what
Gwyn had in mind. I've been hoping he would elaborate, but in the meantime
I am going with my best guess. Criticism is certainly welcome.
- Next message: infobahn: "Re: [Lit.] Buffer overruns"
- Previous message: Tim Smith: "Re: Reality check, surrogate factoring"
- In reply to: infobahn: "Re: [Lit.] Buffer overruns"
- Next in thread: infobahn: "Re: [Lit.] Buffer overruns"
- Reply: infobahn: "Re: [Lit.] Buffer overruns"
- Reply: CuriousCat: "Re: [Lit.] Buffer overruns"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|