public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* C++: No Warning for passing value > 255 as parameter to a function requiring "unsigned char"
@ 2005-07-22 13:51 Martin Wodok
  2005-07-22 14:47 ` Gabriel Dos Reis
  2005-07-22 15:22 ` Eljay Love-Jensen
  0 siblings, 2 replies; 8+ messages in thread
From: Martin Wodok @ 2005-07-22 13:51 UTC (permalink / raw)
  To: gcc-help

Hi,

I'm compiling with "gcc (GCC) 4.0.1 (Debian 4.0.1-2)" and have the following 
problem: I don't manage to get a warning for the following code

    void anyFunction (unsigned char var)
    {
      printf ("anyFunction's var: %d\n", var); fflush(0);
    }

    int main()
    {
      unsigned char var = 1000;
      printf ("main's var: %d\n", var); fflush(0);
      anyFunction (1000);
    }

Assigning 1000 is of course out of bounds for "unsigned char", thus the 
correct output, which I do get, is:

    main's var: 232
    anyFunction's var: 232

My parameters to enable warnings are:

-Wall -Winline -Wshadow -Woverloaded-virtual -W -Wconversion -Wpointer-arith

Do I miss some warning-parameters that I can use to get a warning for the 
above code? I haven't found any yet...

Many thanks in advance!
Greetings from Munich,
Martin Wodok

-- 
Dipl.-Inf. Martin Wodok
OSB AG Ingenieur- und IT-Dienstleistungen
Klenzestraße 38
80469 München
Telefon:  +49 (0)89/23 88 57-48
Telefax:  +49 (0)89/23 88 57-40

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: C++: No Warning for passing value > 255 as parameter to a function requiring "unsigned char"
  2005-07-22 13:51 C++: No Warning for passing value > 255 as parameter to a function requiring "unsigned char" Martin Wodok
@ 2005-07-22 14:47 ` Gabriel Dos Reis
  2005-07-26  9:36   ` Martin Wodok
  2005-07-22 15:22 ` Eljay Love-Jensen
  1 sibling, 1 reply; 8+ messages in thread
From: Gabriel Dos Reis @ 2005-07-22 14:47 UTC (permalink / raw)
  To: Martin Wodok; +Cc: gcc-help

Martin Wodok <m.wodok@osb-ag.de> writes:

| Assigning 1000 is of course out of bounds for "unsigned char", thus the 

No, it is not.  Unsigned integer types are modulo types; it is no overflow.

-- Gaby

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: C++: No Warning for passing value > 255 as parameter to a function requiring "unsigned char"
  2005-07-22 13:51 C++: No Warning for passing value > 255 as parameter to a function requiring "unsigned char" Martin Wodok
  2005-07-22 14:47 ` Gabriel Dos Reis
@ 2005-07-22 15:22 ` Eljay Love-Jensen
  2005-07-22 15:24   ` Eljay Love-Jensen
  1 sibling, 1 reply; 8+ messages in thread
From: Eljay Love-Jensen @ 2005-07-22 15:22 UTC (permalink / raw)
  To: Martin Wodok, gcc-help

Hi Martin,

Change this...

unsigned char var = 1000;

...to this...

unsigned char var = MakeUnsignedChar(1000);

And add the function:

#include <stdio.h>
#include <stdlib.h>
unsigned char MakeUnsignedCharFromInt(int x)
{
  if(x > 255 || x < 0)
  {
      fprintf(stderr, "Parameter %d out of unsigned char bounds.", x);
      abort();
  }
  return (unsigned char)x;
}

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: C++: No Warning for passing value > 255 as parameter to a function requiring "unsigned char"
  2005-07-22 15:22 ` Eljay Love-Jensen
@ 2005-07-22 15:24   ` Eljay Love-Jensen
  0 siblings, 0 replies; 8+ messages in thread
From: Eljay Love-Jensen @ 2005-07-22 15:24 UTC (permalink / raw)
  To: Martin Wodok, gcc-help

correction...

unsigned char var = MakeUnsignedChar(1000);

...should be...

unsigned char var = MakeUnsignedCharFromInt(1000);

--Eljay

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: C++: No Warning for passing value > 255 as parameter to a function requiring "unsigned char"
  2005-07-22 14:47 ` Gabriel Dos Reis
@ 2005-07-26  9:36   ` Martin Wodok
  2005-07-26 14:28     ` Eljay Love-Jensen
  2005-07-26 21:54     ` Gabriel Dos Reis
  0 siblings, 2 replies; 8+ messages in thread
From: Martin Wodok @ 2005-07-26  9:36 UTC (permalink / raw)
  To: gcc-help

Hi Gaby
hi Eljay,

thanks for your answer!

> Martin Wodok <m.wodok@osb-ag.de> writes:
> | Assigning 1000 is of course out of bounds for "unsigned char", thus the
>
> No, it is not.  Unsigned integer types are modulo types; it is no overflow.

Yes, they're modulo types, that's why this is NOT an error, but I still think 
that should be a WARNING. There're compilers like Tasking that throw a 
warning out on that one, so I'm really searching for a way to enable a 
warning for that one (maybe something like the warning -Wconversion), and not 
some extra code (Eljay, thanks anyway!!), because it should work on existing 
code and I don't want to get code-explosion (we're working embedded) just 
because some range-check that could be done by the compiler...

So it seems there's really no way for GCC to warn here?

Thanks again Gaby and Eljay,
/\/\artin

-- 
Dipl.-Inf. Martin Wodok
OSB AG Ingenieur- und IT-Dienstleistungen
Klenzestraße 38
80469 München
Telefon:  +49 (0)89/23 88 57-48
Telefax:  +49 (0)89/23 88 57-40

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: C++: No Warning for passing value > 255 as parameter to a function requiring "unsigned char"
  2005-07-26  9:36   ` Martin Wodok
@ 2005-07-26 14:28     ` Eljay Love-Jensen
  2005-07-26 21:54     ` Gabriel Dos Reis
  1 sibling, 0 replies; 8+ messages in thread
From: Eljay Love-Jensen @ 2005-07-26 14:28 UTC (permalink / raw)
  To: Martin Wodok, gcc-help

Hi Martin,

The extra code could be enabled for a special build, and macro functioned
away for regular (debug and release) builds.

HTH,
--Eljay


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: C++: No Warning for passing value > 255 as parameter to a function requiring "unsigned char"
  2005-07-26  9:36   ` Martin Wodok
  2005-07-26 14:28     ` Eljay Love-Jensen
@ 2005-07-26 21:54     ` Gabriel Dos Reis
  2005-07-27  0:44       ` Sisyphus
  1 sibling, 1 reply; 8+ messages in thread
From: Gabriel Dos Reis @ 2005-07-26 21:54 UTC (permalink / raw)
  To: Martin Wodok; +Cc: gcc-help

Martin Wodok <m.wodok@osb-ag.de> writes:

| Hi Gaby
| hi Eljay,
| 
| thanks for your answer!
| 
| > Martin Wodok <m.wodok@osb-ag.de> writes:
| > | Assigning 1000 is of course out of bounds for "unsigned char", thus the
| >
| > No, it is not.  Unsigned integer types are modulo types; it is no overflow.
| 
| Yes, they're modulo types, that's why this is NOT an error,

The argument was that it makes sense to issue a warning when the the
code is dubious -- not just an error.  However, in this case, modulo
arithmetic for unsigned int is a characteristic propery.
Consequently, it does not make much sense to warn about it.

-- Gaby

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: C++: No Warning for passing value > 255 as parameter to a function requiring "unsigned char"
  2005-07-26 21:54     ` Gabriel Dos Reis
@ 2005-07-27  0:44       ` Sisyphus
  0 siblings, 0 replies; 8+ messages in thread
From: Sisyphus @ 2005-07-27  0:44 UTC (permalink / raw)
  Cc: gcc-help


----- Original Message ----- 
From: "Gabriel Dos Reis"

> |
> | > Martin Wodok <m.wodok@osb-ag.de> writes:
> | > | Assigning 1000 is of course out of bounds for "unsigned char", thus
the
> | >
> | > No, it is not.  Unsigned integer types are modulo types; it is no
overflow.
> |
> | Yes, they're modulo types, that's why this is NOT an error,
>
> The argument was that it makes sense to issue a warning when the the
> code is dubious -- not just an error.  However, in this case, modulo
> arithmetic for unsigned int is a characteristic propery.
> Consequently, it does not make much sense to warn about it.
>

Yet gcc (as distinct from g++) does issue a warning about this. At least
that's the case for me with versions 3.2.2 and 3.4.4. Is that an instance of
gcc doing something that doesn't make much sense ?

That's not a rhetorical question, btw. I anticipate that your answer is
"yes" - but faik there may well be mitigating circumstances that I'm unaware
of.

Cheers,
Rob

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2005-07-27  0:44 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-07-22 13:51 C++: No Warning for passing value > 255 as parameter to a function requiring "unsigned char" Martin Wodok
2005-07-22 14:47 ` Gabriel Dos Reis
2005-07-26  9:36   ` Martin Wodok
2005-07-26 14:28     ` Eljay Love-Jensen
2005-07-26 21:54     ` Gabriel Dos Reis
2005-07-27  0:44       ` Sisyphus
2005-07-22 15:22 ` Eljay Love-Jensen
2005-07-22 15:24   ` Eljay Love-Jensen

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 read-only IMAP folder(s) and NNTP newsgroup(s).