RE: [ROOT] compiler warning

From: Valeri Fine (fine@bnl.gov)
Date: Sun Jun 06 2004 - 23:47:29 MEST


> 
> Dear Elias, dear Valeri
> 
> Thank you for your explanation.
> I have now solved the problem by using:
>     Int_t csize = strlen(opt) + 1;
>     char option[csize];
> 
[Valeri Fine] 
 Let me call your attention that your solution is still error prone

 You allocated the needed space on stack.
 This means the space will be automatically de-allocated and (it can be
allocated for other purpose) as soon as your code leaves the "option"
variable scope. Because you did not provide your source I have no clue
what this scope is. May be it is what you wanted . . . 

> I hope that this does not only work with gcc but also with
> other compilers.
[Valeri Fine] 
I hope thee smart compiler will warn as soon as you try using this
variable out of scope, however the smart guy always can defeat any
compiler :-)

Hope this helps.

> 
> Best regards
> Christian
> 
> 
> Valeri Fine wrote:
> 
> > Hello Christian
> >
> >
> >
> >>Dear Rooters
> >>
> >>I apologize for a simple non-root question, but sorrowly I have
> >>nobody else to ask:
> >>Sometimes I have statements such as the following:
> >>    if (strcmp(name, "option") == 0) {
> >>       char *option;
> >>       option = strcpy(option, opt);
> >>    }
> >>
> >>Although it is correct, I get the following compiler warning:
> >>"warning: `char*option' might be used uninitialized in this
function"
> >
> >
> > [Valeri Fine] It looks like the compiler pointed you to your bug to
be
> > fixed. That must lead to the memory corruption. "strcpy"  alone
> > allocates NO extra memory.
> >
> >
> >
> >>How can I prevent such compiler warnings?
> >
> > [Valeri Fine]
> >
> >>Would it be save to write:
> >>       char *option = "";
> >>(If this is not save, why?)
> >
> > [Valeri Fine]
> > Not it is NOT safe !!!
> >
> > To avoid the memory corruption you need
> >
> >   char *option = new char[strlen(opt)+1];
> >
> >  or something like this to make sure the memory the POINTER "option"
> > points to is allocated and it is enough to accommodate the entire
string
> > pointed by "opt" pointer.
> >
> >   Hope this helps.
> >
> >    Valeri
> >
> >>Thank you in advance
> >>Best regards
> >>Christian
> >>-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> >>C.h.r.i.s.t.i.a.n. .S.t.r.a.t.o.w.a
> >>V.i.e.n.n.a.         .A.u.s.t.r.i.a
> >>-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> >
> >
> >
> >



This archive was generated by hypermail 2b29 : Sun Jan 02 2005 - 05:50:08 MET