groff
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Groff] Readable tbl-source


From: Ralph Corderoy
Subject: Re: [Groff] Readable tbl-source
Date: Mon, 09 Sep 2002 13:34:34 +0100

Hi Werner,

Thanks for the clarification of a string class.

> >          1  char *string::extract() const
> >          2  {
> >          3    char *p = ptr;
> >          4    int n = len;
> >          5    int nnuls = 0;
> >          6    int i;
> >          7    for (i = 0; i < n; i++)
> >          8      if (p[i] == '\0')
> >          9        nnuls++;
> >         10    char *q = new char[n + 1 - nnuls];
> >         11    char *r = q;
> >         12    for (i = 0; i < n; i++)
> >         13      if (p[i] != '\0')
> >         14        *r++ = p[i];
> >         15    q[n] = '\0';
> >         16    return q;
> >         17  }
> > 
> > Does `new char[n]' give memory pre-filled with '\0'?  I guess not
> > because #15 puts the terminating '\0' in place.  But if on entry ptr ==
> > "a\0c\0" and len == 3
> 
> No.  len == 4 for "a\0b\0".  In a `string' class, strings aren't
> null-terminated; if there is a trailing \0, it is part of the string.
> 
> > [...]

OK, but the bug description you've snipped is still valid.  If nnuls is
anything but 0 then #15 is writing past the end of allocated memory
since q is `n + 1 - nnuls' long.  Only when q is `n + 1' long is it
valid to write to q[n].  #15 should be `*r = '\0';', no?

I'm still thinking about remove_spaces()  :-)


Ralph.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]