Re: code style in the vfs



On Sun, Sep 26, 2004 at 12:48:19PM +0200, Leonard den Ottolander wrote:
> On Sun, 2004-09-26 at 10:30, Pavel Tsekov wrote:
> > > char *dirname (const char *path)
> > > {
> > >      char *slash = strrchr (path, '/');
> > >      if (slash)
> > >          *slash = '\0';
> > >      return path;
> > > }
> > >
> > > No compiler could ever warn you about this "const-away" cast, and if you
> > > pass a string literal to it, the behaviour is undefined.
> > 
> > I know that the compiler won't detect it.
> 
> Actually it does (for this particular piece of code). Without a cast on
> the return value that code *does* produce a warning (return discards
> qualifiers from pointer target type).
> 
right, but it's the wrong place. change the return value to const char *
and no compiler will warn about the bug.
we want a warning at *slash = 0;

On Sun, Sep 26, 2004 at 10:30:02AM +0200, Pavel Tsekov wrote:
> If you point is to use those macros to detect misuse of certain
> functions may I suggest that you think of implementig some clean, more
> general method which could be enabled/disabled by the programmer at
> his choice, so that he can test its code for such errors. In short I
> (for example) don't want to write in one case `mod_funcname' and in
> the other just `funcname'. I prefer to write `funcname' and at some
> point I turn on a switch and the code is automagically tested for
> incorrect usage of `funcname'. Now it is `strchr' then it will be some
> other function, the approach that you suggest would easily end up in a
> mess.
>
i think you deeply misunderstand or at least underestimate the issue.
guess why c++ has "const in, const out" and a "non-const in, non-const
out" versions of these functions? in c one can't overload, so one has to
accept some inconvenience to produce safe code.

-- 
Hi! I'm a .signature virus! Copy me into your ~/.signature, please!
--
Chaos, panic, and disorder - my work here is done.



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