Re: sprintf and clists



On Tue, 25 Jun 2002, Jean-Christophe Berthon wrote:


err, ... sorry for the sizeof(int) shit. I misunderstood this.
?I'm not too sure about the '\0' question. I mean what would happen if the
non initialized buffer variable contains no '\0' character? gtk_clist_append()
(or even to strdup() ?) might be using a call to strlen() for example and
couldn't this be the source of some problem?
M.
>
> >
> > > gchar buffer[1024];
> > > static gchar *cclist[1];
> > >
> > > sprintf(buffer, "%d", some_dumb_int);
> > > cclist[0] = strdup(buffer);
> > > gtk_clist_append(GTK_CLIST(clist1), cclist);
> > >
> > > This is assuming you have a 1 column clist.
> > >
> >
> > check out if the string is '\0' terminated. best initialize it after the
> > declaration with memset(buffer,'\0',1024);
>
> I don't think you need to check for the '\0' as the buffer is large enough
> to hold an integer. An integer might be at max 32bits, so for an unisgned
> int, approx. 4 000 000 000 (4 billion) or 4.0e9. So there is a maximum of 10
> digits or caracteres. As the buffer is 1024 large, it is surelly big enough
> not to risk to have a buffer overflow.
>
> > BTW wouldn't it be enough to use a buffer of size "sizeof(int)" ?
>
> No you cannot do that. Sizeof(int) give the size in octet allocate for one
> integer, depending on the compiler and the machine, it might be 2 or 4
> octets. If you declare your buffer as sizeof(int), then at max you have a
> table of 4 caracteres. So you cannot put 1000 for example into such a
> buffer, it's too big number (it's 4 digits! so there is no place left for
> the '\0')
>
> Jean-Christophe
>
>
>
> _______________________________________________
> gtk-list mailing list
> gtk-list gnome org
> http://mail.gnome.org/mailman/listinfo/gtk-list
>

 ____                 _____
|  _ \  ___ ___  _ __| ____|
| | | |/ __/ _ \| '__|  _|
| |_| | (_| (_) | |  | |___
|____/ \___\___/|_|  |_____|





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