Re: [Lit.] Buffer overruns

From: David Wagner (daw_at_taverner.cs.berkeley.edu)
Date: 01/26/05


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.



Relevant Pages

  • Re: [Lit.] Buffer overruns
    ... > The suggestion came from Doug Gwyn. ... Whenever requesting a resource, you ought to check that the request ...
    (sci.crypt)
  • Re: Is there "an unconscious"?
    ... >> Tim McNamara wrote: ... >>> mind etc were born from the fear of death, ... > self-induced hallucinations are as good an explanation as any. ... in the mind out of a sufficient suggestion in the environment? ...
    (sci.psychology.psychotherapy.moderated)
  • Re: Bards Fascination ability: broken?
    ... They chose to use their abilities to get what ... > They're not role-playing, but never mind that. ... I don't care if they ... they're using fascinate and suggestion. ...
    (rec.games.frp.dnd)
  • Re: Hide Selected Objects
    ... Following up on Gord's suggestion here is code that will hide the selected ... objects, in this case shapes: ... Keep in mind that you can't reverse this by writing ... Selectable objects you have inserted like drawing objects or pictures? ...
    (microsoft.public.excel.worksheet.functions)
  • Re: Streptococcus suis - China - latest
    ... >>> of the government as to the details ... I didn't realise you weren't a citizen of an EU member state ... own suggestion, but never mind. ...
    (uk.business.agriculture)

Quantcast