public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* gcc strcpy pseudo bug or detail
@ 2002-10-17  9:40 Mauricio Labra
  2002-10-17 12:01 ` Andreas Schwab
  2002-10-18 16:26 ` Phil Edwards
  0 siblings, 2 replies; 3+ messages in thread
From: Mauricio Labra @ 2002-10-17  9:40 UTC (permalink / raw)
  To: bug-gcc; +Cc: gcc

Hi, sorry for my english...

I have a RH 7.2 and detect this detail (in C example):

# rpm -qi gcc
Name        : gcc                          Relocations: (not relocateable)
Version     : 2.96                              Vendor: Red Hat, Inc.
Release     : 98                            Build Date: mar 04 sep 2001 14:10:42 CLT
Install date: mar 09 jul 2002 07:53:37 CLT      Build Host: stripples.devel.redhat.com
Group       : Development/Languages         Source RPM: gcc-2.96-98.src.rpm
Size        : 8376529                          License: GPL
Packager    : Red Hat, Inc. <http://bugzilla.redhat.com/bugzilla>
URL         : http://gcc.gnu.org
Summary     : Various compilers (C, C++, Objective-C, Java, ...)
Description :
The gcc package contains the GNU Compiler Collection: cc and gcc. You'll need
this package in order to compile C/C++ code.


Source sample program x.c :

#include <stdio.h>
#include <string.h>

int main()
{
        char tmp[80];

        strcpy(tmp,msg());
        puts(tmp);
        return(0);
}

char *msg()
{
        return("Mensaje de prueba");
}

This sample have a warnings...

# gcc -O3 -Wall x.c
x.c: In function `main':
x.c:9: warning: implicit declaration of function `msg'
x.c:9: warning: passing arg 1 of `strlen' makes pointer from integer without a cast
x.c:9: warning: passing arg 1 of `strlen' makes pointer from integer without a cast
x.c:9: warning: passing arg 1 of `strlen' makes pointer from integer without a cast
x.c:9: warning: passing arg 2 of `memcpy' makes pointer from integer without a cast
x.c:9: warning: passing arg 2 of `strcpy' makes pointer from integer without a cast
x.c: At top level:
x.c:15: warning: type mismatch with previous implicit declaration
x.c:9: warning: previous implicit declaration of `msg'
x.c:15: warning: `msg' was previously implicitly declared to return `int'


Whats are the warnings "makes pointer from integer without a cast" of strlen or memcpy functions?
I don't use this functions !

#gcc -O3 -Wall -E x.c
...
...
...
int main()
{
        char tmp[80];

        (__extension__ (__builtin_constant_p (msg()) ? (((size_t)(const void *)((msg()) + 1) - (size_t)(const
 void *)(msg()) == 1) && strlen (msg()) + 1 <= 8 ? __strcpy_small (tmp, __extension__ (((__const unsigned cha
r *) (__const char *) (msg()))[0 + 1] << 8 | ((__const unsigned char *) (__const char *) (msg()))[0]), __exte
nsion__ (((__const unsigned char *) (__const char *) (msg()))[4 + 1] << 8 | ((__const unsigned char *) (__con
st char *) (msg()))[4]), __extension__ (((((__const unsigned char *) (__const char *) (msg()))[0 + 3] << 8 |
((__const unsigned char *) (__const char *) (msg()))[0 + 2]) << 8 | ((__const unsigned char *) (__const char
*) (msg()))[0 + 1]) << 8 | ((__const unsigned char *) (__const char *) (msg()))[0]), __extension__ (((((__con
st unsigned char *) (__const char *) (msg()))[4 + 3] << 8 | ((__const unsigned char *) (__const char *) (msg(
)))[4 + 2]) << 8 | ((__const unsigned char *) (__const char *) (msg()))[4 + 1]) << 8 | ((__const unsigned cha
r *) (__const char *) (msg()))[4]), strlen (msg()) + 1) : (char *) memcpy (tmp, msg(), strlen (msg()) + 1)) :
 strcpy (tmp, msg())));
        puts(tmp);
        return(0);
}

char *msg()
{
        return("Mensaje de prueba");
}

Aha!!   Why gcc transform a simple strcpy in this macro code ?
The strcpy with function argument are dangerous...!!!
this execute many times the function !!!

Solution :

#include <stdio.h>
#include <string.h>

char *msg()
{
        return("Mensaje de prueba");
}

int main()
{
        char tmp[80];
        char *aux;

        aux=msg();
        strcpy(tmp,aux);
//      Esto es mas simple
//      memcpy(tmp,msg(),strlen(msg())+1);
        puts(tmp);
        return(0);
}

#gcc -O3 -Wall x.c
No warnings.

#gcc -O3 -Wall -E x.c
...
...
char *msg()
{
        return("Mensaje de prueba");
}

int main()
{
        char tmp[80];
        char *aux;

        aux=msg();
        (__extension__ (__builtin_constant_p (aux) ? (((size_t)(const void *)((aux) + 1) - (size_t)(const void *)(aux) == 1) && strlen (aux) + 1 <= 8 ? __strcpy_small (tmp, __extension__ (((__const unsigned char *) (__const char *) (aux))[0 + 1] << 8 | ((__const unsigned char *) (__const char *) (aux))[0]), __extension__ (((__const unsigned char *) (__const char *) (aux))[4 + 1] << 8 | ((__const unsigned char *) (__const char *) (aux))[4]), __extension__ (((((__const unsigned char *) (__const char *) (aux))[0 + 3] << 8 | ((__const unsigned char *) (__const char *) (aux))[0 + 2]) << 8 | ((__const unsigned char *) (__const char *) (aux))[0 + 1]) << 8 | ((__const unsigned char *) (__const char *) (aux))[0]), __extension__ (((((__const unsigned char *) (__const char *) (aux))[4 + 3] << 8 | ((__const unsigned char *) (__const char *) (aux))[4 + 2]) << 8 | ((__const unsigned char *) (__const char *) (aux))[4 + 1]) << 8 | ((__const unsigned char *) (__const char *) (aux))[4]), strlen (aux) + 1) : (char *) memcpy (tmp, aux, strlen (aux) + 1)) : strcpy (tmp, aux)));
        puts(tmp);
        return(0);
}


Please, tell me what is this and/or how to skip this macro...
and what other functions use this kind of macro control ?
I use string functions very well, and don't need extra code.

Thanxz.

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

* Re: gcc strcpy pseudo bug or detail
  2002-10-17  9:40 gcc strcpy pseudo bug or detail Mauricio Labra
@ 2002-10-17 12:01 ` Andreas Schwab
  2002-10-18 16:26 ` Phil Edwards
  1 sibling, 0 replies; 3+ messages in thread
From: Andreas Schwab @ 2002-10-17 12:01 UTC (permalink / raw)
  To: Mauricio Labra; +Cc: bug-gcc, gcc

"Mauricio Labra" <mauricio.labra@construmart.cl> writes:

|> Source sample program x.c :
|> 
|> #include <stdio.h>
|> #include <string.h>
|> 
|> int main()
|> {
|>         char tmp[80];
|> 
|>         strcpy(tmp,msg());
|>         puts(tmp);
|>         return(0);
|> }
|> 
|> char *msg()
|> {
|>         return("Mensaje de prueba");
|> }
|> 
|> This sample have a warnings...
|> 
|> # gcc -O3 -Wall x.c
|> x.c: In function `main':
|> x.c:9: warning: implicit declaration of function `msg'

Fix your code and properly declare the function.

Andreas.

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE Linux AG, Deutschherrnstr. 15-19, D-90429 Nürnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

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

* Re: gcc strcpy pseudo bug or detail
  2002-10-17  9:40 gcc strcpy pseudo bug or detail Mauricio Labra
  2002-10-17 12:01 ` Andreas Schwab
@ 2002-10-18 16:26 ` Phil Edwards
  1 sibling, 0 replies; 3+ messages in thread
From: Phil Edwards @ 2002-10-18 16:26 UTC (permalink / raw)
  To: Mauricio Labra; +Cc: bug-gcc, gcc

On Thu, Oct 17, 2002 at 11:21:52AM -0400, Mauricio Labra wrote:
> 
> #include <stdio.h>
> #include <string.h>
> 
> int main()
> {
>         char tmp[80];
> 
>         strcpy(tmp,msg());
>         puts(tmp);
>         return(0);
> }
> 
> char *msg()
> {
>         return("Mensaje de prueba");
> }
> 
> This sample have a warnings...
> 
> # gcc -O3 -Wall x.c

These warnings:

> x.c: In function `main':
> x.c:9: warning: implicit declaration of function `msg'
> x.c:9: warning: passing arg 1 of `strlen' makes pointer from integer without a cast
> x.c:9: warning: passing arg 1 of `strlen' makes pointer from integer without a cast
> x.c:9: warning: passing arg 1 of `strlen' makes pointer from integer without a cast
> x.c:9: warning: passing arg 2 of `memcpy' makes pointer from integer without a cast
> x.c:9: warning: passing arg 2 of `strcpy' makes pointer from integer without a cast

are explained by this one:

> x.c: At top level:
> x.c:15: warning: type mismatch with previous implicit declaration
> x.c:9: warning: previous implicit declaration of `msg'
> x.c:15: warning: `msg' was previously implicitly declared to return `int'
> 
> 
> Whats are the warnings "makes pointer from integer without a cast" of strlen or memcpy functions?
> I don't use this functions !

This is the wrong mailing list to ask such questions; you need a book on
how to program C.  (Hint:  at line 9, msg() has not been declared, so the
C language assumes it returns int.)

-- 
I would therefore like to posit that computing's central challenge, viz. "How
not to make a mess of it," has /not/ been met.
                                                 - Edsger Dijkstra, 1930-2002

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

end of thread, other threads:[~2002-10-18 21:37 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-10-17  9:40 gcc strcpy pseudo bug or detail Mauricio Labra
2002-10-17 12:01 ` Andreas Schwab
2002-10-18 16:26 ` Phil Edwards

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).