LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Sergei Organov <osv@javad.com>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: "J.A. MagallÃón" <jamagallon@ono.com>,
	"Jan Engelhardt" <jengelh@linux01.gwdg.de>,
	"Jeff Garzik" <jeff@garzik.org>,
	"Linux Kernel Mailing List" <linux-kernel@vger.kernel.org>,
	"Andrew Morton" <akpm@linux-foundation.org>
Subject: Re: somebody dropped a (warning) bomb
Date: Tue, 13 Feb 2007 21:06:24 +0300	[thread overview]
Message-ID: <87ps8end9b.fsf@javad.com> (raw)
In-Reply-To: <Pine.LNX.4.64.0702120804520.8424@woody.linux-foundation.org>

Linus Torvalds <torvalds@linux-foundation.org> writes:
> On Mon, 12 Feb 2007, Sergei Organov wrote:
>> 
>> Why strlen() should be allowed to be called with an incompatible pointer
>> type? My point is that gcc should issue *different warning*, -- the same
>> warning it issues here:
>
> I agree that "strlen()" per se isn't different.

I agree that making strxxx() family special is not a good idea. So what
do we do for a random foo(char*) called with an 'unsigned char*'
argument? Silence? Hmmm... It's not immediately obvious that it's indeed
harmless. Yet another -Wxxx option to GCC to silence this particular
case?

> A warnign is only as good as
>  (a) the thing it warns about
>  (b) the thing you can do about it
> 
> And THAT is the fundamental problem with that *idiotic* warning. Yes, it's 
> technically correct. Yes, it's "proper C grammar". But if you can't get 
> over the hump of realizing that there is a difference between "grammar" 
> and "intelligent speech", you shouldn't be doing compilers.
> 
> So the warning sucks because:

May I suggest another definition for a warning being entirely sucks?
"The warning is entirely sucks if and only if it never has true
positives." In all other cases it's only more or less sucks, IMHO.

>  - the thing it warns about (passing "unsigned char" to something that 
>    doesn't specify a sign at all!) is not something that sounds wrong in 
>    the first place. Yes, it's unsigned. But no, the thing it is passed to 
>    didn't specify that it wanted a "signed" thing in the first place. The 
>    "strlen()" function literally says
>
> 	"I want a char of indeterminate sign"!

I'm afraid I don't follow. Do we have a way to say "I want an int of
indeterminate sign" in C? The same way there doesn't seem to be a way
to say "I want a char of indeterminate sign". :( So no, strlen() doesn't
actually say that, no matter if we like it or not. It actually says "I
want a char with implementation-defined sign".

>    which implies that strlen really doesn't care about the sign. The same 
>    is true of *any* function that takes a "char *". Such a function 
>    doesn't care, and fundamentally CANNOT care about the sign, since it's 
>    not even defined!

In fact it's implementation-defined, and this may make a difference
here. strlen(), being part of C library, could be specifically
implemented for given architecture, and as architecture is free to
define the sign of "char", strlen() could in theory rely on particular
sign of "char" as defined for given architecture. [Not that I think that
any strlen() implementation actually depends on sign.]

>    So the warning fails the (a) criterion. The warning isn't valid, 
>    because the thing it warns about isn't a valid problem!

Can we assure that no function taking 'char*' ever cares about the sign?
I'm not sure, and I'm not a language lawyer, but if it's indeed the
case, I'd probably agree that it might be a good idea for GCC to extend
the C language so that function argument declared "char*" means either
of "char*", "signed char*", or "unsigned char*" even though there is no
precedent in the language.

BTW, the next logical step would be for "int*" argument to stop meaning
"signed int*" and become any of "int*", "signed int*" or "unsigned
int*". Isn't it cool to be able to declare a function that won't produce
warning no matter what int is passed to it? ;)

>  - it _also_ fails the (b) criterion, because quite often there is nothing 
>    you can do about it. Yes, you can add a cast, but adding a cast 
>    actually causes _worse_ code (but the warning is certainly gone). But 
>    that makes the _other_ argument for the warning totally point-less: if 
>    the reason for the warning was "bad code", then having the warning is 
>    actively BAD, because the end result is actually "worse code".
>
> See? The second point is why it's important to also realize that there is 
> a lot of real and valid code that actually _does_ pass "strlen()" an 
> unsigned string.

And here we finally get to the practice...

> There are tons of reasons for that to happen: the part of the program
> that _does_ care wants to use a "unsigned char" array, because it ends
> up doing things like "isspace(array[x])", and that is not well-defined
> if you use a "char *" array.

Yes, indeed. So the real problem of the C language is inconsistency
between strxxx() and isxxx() families of functions? If so, what is 
wrong with actually fixing the problem, say, by using wrappers over
isxxx()? Checking... The kernel already uses isxxx() that are macros
that do conversion to "unsigned char" themselves, and a few invocations
of isspace() I've checked pass "char" as argument. So that's not a real
problem for the kernel, right?

> So there are lots of reasons to use "unsigned char" arrays for strings. 
> Look it up. Look up any half-way reasonable man-page for the "isspace()" 
> kind of functions, and if they don't actually explicitly say that you 
> should use unsigned characters for it, those man-pages are crap.

Well, even C99 itself does say it. But the kernel already handles this
issue nicely without resorting to requirement to convert char to
unsigned char, isn't it?

> Because those functions really *are* defined in "int", but it's the
> same kind of namespace that "getchar()" works in (ie "unsigned char" +
> EOF, where EOF _usually_ is -1, although other values are certainly
> technically legal too).
>
> So:
>
>  - in practice, a lot of "good programming" uses "unsigned char" pointers 
>    for doing strings. There are LOTS of reasons for that, but "isspace()" 
>    and friends is the most obvious one.

As the isxxx() family does not seem to be a real problem, at least in
the context of the kernel source base, I'd like to learn other reasons
to use "unsigned char" for doing strings either in general or
specifically in the Linux kernel.

>  - if you can't call "strlen()" on your strings without the compiler 
>    warning, there's two choices: the compiler warning is CRAP, or your 
>    program is bad. But as I just showed you, "unsigned char *" is actually 
>    often the *right* thing to use for string work, so it clearly wasn't 
>    the program that was bad.

OK, provided there are actually sound reasons to use "unsigned char*"
for strings, isn't it safer to always use it, and re-define strxxx() for
the kernel to take that one as argument? Or are there reasons to use
"char*" (or "signed char*") for strings as well? I'm afraid that if two
or three types are allowed to be used for strings, some inconsistencies
here or there will remain no matter what.

Another option could be to always use "char*" for strings and always compile
the kernel with -funsigned-char. At least it will be consistent with the
C idea of strings being "char*".

-- Sergei.

  reply	other threads:[~2007-02-13 18:06 UTC|newest]

Thread overview: 68+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-02-08 15:00 Jeff Garzik
2007-02-08 16:33 ` Linus Torvalds
2007-02-08 18:42   ` Jan Engelhardt
2007-02-08 19:53     ` Linus Torvalds
2007-02-08 21:10       ` Jan Engelhardt
2007-02-08 21:37         ` Linus Torvalds
2007-02-08 23:12           ` David Rientjes
2007-02-08 23:37             ` Linus Torvalds
2007-02-09  0:24               ` David Rientjes
2007-02-09  0:42                 ` Linus Torvalds
2007-02-09  0:59                   ` Linus Torvalds
2007-02-09  0:59                   ` David Rientjes
2007-02-09  1:11                     ` Linus Torvalds
2007-02-09  1:18                       ` David Rientjes
2007-02-09 15:38                         ` Linus Torvalds
2007-02-09  3:27                   ` D. Hazelton
2007-02-09 19:54                     ` Pete Zaitcev
2007-02-09 12:34                   ` Jan Engelhardt
2007-02-09 13:16                     ` linux-os (Dick Johnson)
2007-02-09 17:45                       ` Jan Engelhardt
2007-02-09 20:29                         ` linux-os (Dick Johnson)
2007-02-09 22:05                           ` Jan Engelhardt
2007-02-09 22:58                             ` Martin Mares
2007-02-12 18:50                             ` linux-os (Dick Johnson)
2007-02-13 15:14                     ` Dick Streefland
2007-02-08 21:13       ` J.A. Magallón
2007-02-08 21:42         ` Linus Torvalds
2007-02-08 22:03           ` Linus Torvalds
2007-02-08 22:19             ` Willy Tarreau
2007-02-09  0:03             ` J.A. Magallón
2007-02-09  0:22               ` Linus Torvalds
2007-02-09 12:38             ` Sergei Organov
2007-02-09 15:58               ` Linus Torvalds
2007-02-12 11:12                 ` Sergei Organov
2007-02-12 16:26                   ` Linus Torvalds
2007-02-13 18:06                     ` Sergei Organov [this message]
2007-02-13 18:26                       ` Pekka Enberg
2007-02-13 19:14                         ` Sergei Organov
2007-02-13 19:43                           ` Pekka Enberg
2007-02-13 20:29                             ` Sergei Organov
2007-02-13 21:31                               ` Jeff Garzik
2007-02-13 23:21                               ` Linus Torvalds
2007-02-15 13:20                                 ` Sergei Organov
2007-02-15 15:57                                   ` Linus Torvalds
2007-02-15 18:53                                     ` Sergei Organov
2007-02-15 19:02                                       ` Linus Torvalds
2007-02-15 20:23                                         ` me, not " Oleg Verych
2007-02-16  4:26                                         ` Rene Herman
2007-02-19 11:58                                           ` Sergei Organov
2007-02-19 13:58                                         ` Sergei Organov
2007-02-15 22:32                                     ` Lennart Sorensen
2007-02-13 19:25                       ` Linus Torvalds
2007-02-13 19:59                         ` Sergei Organov
2007-02-13 20:24                           ` Linus Torvalds
2007-02-15 15:15                             ` Sergei Organov
2007-02-13 21:13                         ` Rob Landley
2007-02-13 22:21                       ` Olivier Galibert
2007-02-14 12:52                         ` Sergei Organov
2007-02-15 20:06                         ` Sergei Organov
2007-02-09 15:10     ` Sergei Organov
2007-02-08 16:35 ` Kumar Gala
     [not found] <7Mj5f-3oz-21@gated-at.bofh.it>
     [not found] ` <7MktH-5EW-35@gated-at.bofh.it>
     [not found]   ` <7Mmvy-vj-17@gated-at.bofh.it>
     [not found]     ` <7MnBC-2fk-13@gated-at.bofh.it>
     [not found]       ` <7MoQx-4p8-11@gated-at.bofh.it>
     [not found]         ` <7MpjE-50z-7@gated-at.bofh.it>
     [not found]           ` <7MpCS-5Fe-9@gated-at.bofh.it>
     [not found]             ` <7MDd7-17w-1@gated-at.bofh.it>
     [not found]               ` <7MGkB-62k-31@gated-at.bofh.it>
     [not found]                 ` <7NHoe-2Mb-37@gated-at.bofh.it>
     [not found]                   ` <7NMe9-1ZN-7@gated-at.bofh.it>
     [not found]                     ` <7Oagl-6bO-1@gated-at.bofh.it>
     [not found]                       ` <7ObvW-89N-23@gated-at.bofh.it>
     [not found]                         ` <7Oc8t-NS-1@gated-at.bofh.it>
2007-02-15 20:08                           ` Bodo Eggert
2007-02-16 11:21                             ` Sergei Organov
2007-02-16 14:51                               ` Bodo Eggert
2007-02-19 11:56                                 ` Sergei Organov
2007-02-16 12:46                             ` Sergei Organov
2007-02-16 17:40                               ` Bodo Eggert
2007-02-19 12:17                                 ` Sergei Organov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87ps8end9b.fsf@javad.com \
    --to=osv@javad.com \
    --cc=akpm@linux-foundation.org \
    --cc=jamagallon@ono.com \
    --cc=jeff@garzik.org \
    --cc=jengelh@linux01.gwdg.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    --subject='Re: somebody dropped a (warning) bomb' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).