public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* A TINY BUG
@ 1997-10-06  1:19 Tage Westlund
  1997-10-06  9:53 ` Jason Zions
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Tage Westlund @ 1997-10-06  1:19 UTC (permalink / raw)
  To: gnu-win32

To gnu designers!
I have found that the following bad code gives "exception" at run
time instead of error message at compilation time (b18 Win95):

#include <stdio.h>
main(){
	printf("%s\n",sizeof(long));
}
Tage

-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

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

* Re: A TINY BUG
  1997-10-06  1:19 A TINY BUG Tage Westlund
@ 1997-10-06  9:53 ` Jason Zions
  1997-10-06 12:27   ` Steve Dum
                     ` (4 more replies)
  1997-10-06 15:47 ` Charles Curley
  1997-10-07  6:18 ` Jon Thackray
  2 siblings, 5 replies; 12+ messages in thread
From: Jason Zions @ 1997-10-06  9:53 UTC (permalink / raw)
  To: tage.westlund; +Cc: gnu-win32

> I have found that the following bad code gives "exception" at run
> time instead of error message at compilation time (b18 Win95):
> 
> #include <stdio.h>
> main(){
>         printf("%s\n",sizeof(long));
> }

No compiler will detect this error at compile-time. The prototype for
printf is (char *, ...); that is, no specific type information for
anything except the first parameter. A compiler would have to read the
first parameter to figure out the expected types for the remaining args,
and much of the time that first parameter is dynamically computed at
runtime instead of being a static string. There are a couple of
lint-like programs that will catch this error with a
compile-time-evaluatable format string, but that's the best you can do.

Summary: learn more about the language before whining about compiler
errors. This is a programmer bug, not a compiler bug.

Jason Zions
Softway Systems Inc., makers of OpenNT
http://www.opennt.com


-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

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

* Re: A TINY BUG
  1997-10-06  9:53 ` Jason Zions
@ 1997-10-06 12:27   ` Steve Dum
  1997-10-06 12:40   ` Matthew Moskewicz
                     ` (3 subsequent siblings)
  4 siblings, 0 replies; 12+ messages in thread
From: Steve Dum @ 1997-10-06 12:27 UTC (permalink / raw)
  To: gnu-win32; +Cc: steve_dum

In message < 34391696.27758E19@softway.com >you write:
>> I have found that the following bad code gives "exception" at run
>> time instead of error message at compilation time (b18 Win95):
>> 
>> #include <stdio.h>
>> main(){
>>         printf("%s\n",sizeof(long));
>> }
>
>No compiler will detect this error at compile-time. The prototype for
>printf is (char *, ...); that is, no specific type information for

Well, actually, if you compile this with a -Wall gcc will diagnose it, and
emit the warning
f.c:3: warning: format argument is not a pointer (arg 2)

steve
--------------
Stephen Dum            steve_dum@mentorg.com    (503)-685-7743
Mentor Graphics Corp.  8005 S.W. Boeckman Rd.   Wilsonville, Or 97070-7777
-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

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

* Re: A TINY BUG
  1997-10-06  9:53 ` Jason Zions
  1997-10-06 12:27   ` Steve Dum
@ 1997-10-06 12:40   ` Matthew Moskewicz
  1997-10-06 14:27   ` Marty Leisner
                     ` (2 subsequent siblings)
  4 siblings, 0 replies; 12+ messages in thread
From: Matthew Moskewicz @ 1997-10-06 12:40 UTC (permalink / raw)
  To: gnu-win32

Jason Zions wrote:
> 
> > I have found that the following bad code gives "exception" at run
> > time instead of error message at compilation time (b18 Win95):
> >
> > #include <stdio.h>
> > main(){
> >         printf("%s\n",sizeof(long));
> > }
> 
> No compiler will detect this error at compile-time. The prototype for
[munch]
> Summary: learn more about the language before whining about compiler
> errors. This is a programmer bug, not a compiler bug.
> 
> Jason Zions
> Softway Systems Inc., makers of OpenNT
> http://www.opennt.com

Jason, you are right in principal, and I hate to muddle 
the issue, but ... :)
gcc will indeed generate a warning for this. Perhaps the 
moral is that one should always turn on all compiler 
warnings.

[example]

sunlab1:~/cs217/scratch> cat test.c

#include<stdio.h>
int main ( void )
{
 
  printf("%s\n",sizeof(long));
  return 0;
}

sunlab1:~/cs217/scratch> gcc -Wall test.c
test.c: In function `main':
test.c:5: warning: format argument is not a pointer (arg 2)

--
 Matthew Moskewicz	|	mailto:moskewcz@Princeton.edu
 24A Holder Hall - PU	|	http://www.Princeton.edu/~moskewcz	
 Princeton, NJ 08544    |
-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

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

* Re: A TINY BUG
  1997-10-06  9:53 ` Jason Zions
  1997-10-06 12:27   ` Steve Dum
  1997-10-06 12:40   ` Matthew Moskewicz
@ 1997-10-06 14:27   ` Marty Leisner
  1997-10-06 17:13   ` Steve Tynor
  1997-10-06 18:01   ` Chris Faylor
  4 siblings, 0 replies; 12+ messages in thread
From: Marty Leisner @ 1997-10-06 14:27 UTC (permalink / raw)
  To: Jason Zions; +Cc: tage.westlund, gnu-win32

I agree, but -Wformat should even catch this (without stdio.h)

Or use -Wall...

> > I have found that the following bad code gives "exception" at run
> > time instead of error message at compilation time (b18 Win95):
> > 
> > #include <stdio.h>
> > main(){
> >         printf("%s\n",sizeof(long));
> > }
> 
> No compiler will detect this error at compile-time. The prototype for
> printf is (char *, ...); that is, no specific type information for
> anything except the first parameter. A compiler would have to read the
> first parameter to figure out the expected types for the remaining args,
> and much of the time that first parameter is dynamically computed at
> runtime instead of being a static string. There are a couple of
> lint-like programs that will catch this error with a
> compile-time-evaluatable format string, but that's the best you can do.
> 
> Summary: learn more about the language before whining about compiler
> errors. This is a programmer bug, not a compiler bug.
> 
> Jason Zions
> Softway Systems Inc., makers of OpenNT
> http://www.opennt.com
> 
> 


-- 
marty
leisner@sdsp.mc.xerox.com  
The Feynman problem solving Algorithm
        1) Write down the problem
        2) Think real hard
        3) Write down the answer
                Murray Gel-mann in the NY Times




-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

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

* Re: A TINY BUG
  1997-10-06  1:19 A TINY BUG Tage Westlund
  1997-10-06  9:53 ` Jason Zions
@ 1997-10-06 15:47 ` Charles Curley
  1997-10-07  6:18 ` Jon Thackray
  2 siblings, 0 replies; 12+ messages in thread
From: Charles Curley @ 1997-10-06 15:47 UTC (permalink / raw)
  To: tage.westlund; +Cc: gnu-win32

At 12:38 AM 10/6/97 +0000, Tage Westlund wrote:
>To gnu designers!
>I have found that the following bad code gives "exception" at run
>time instead of error message at compilation time (b18 Win95):
>
>#include <stdio.h>
>main(){
>	printf("%s\n",sizeof(long));
>}
>Tage

That does not surprise me in the least. A lot of compilers do no type
checking for printf.

Here's the results on Microsoft C++ 4.0:

cd ~/
cl test.c
Microsoft (R) 32-bit C/C++ Standard Compiler Version 10.00.6002 for 80x86
Copyright (C) Microsoft Corp 1984-1996. All rights reserved.

test.c
Microsoft (R) 32-Bit Incremental Linker Version 3.00.5270
Copyright (C) Microsoft Corp 1992-1995. All rights reserved.

/out:test.exe 
test.obj 

Compilation finished at Mon Oct 06 16:32:37


Here's what I got for Microsoft C++ 5.0:



--------------------Configuration: test - Win32 Debug--------------------
Compiling...
test.c
C:\Crc\test.c(4) : warning C4035: 'main' : no return value

test.obj - 0 error(s), 1 warning(s)



Nice of them to warn us about the lack of return value :-)

What happened is this: printf (and its relatives) is a library function
which takes an indeterminate number of arguments, of indeterminate data
type. The compiler and linker cannot scan the inputs for correct typing.

Why, you ask, can't the compiler "know" what printf *should* take, and scan
and test accordingly? The answer is that some programs provide their own
printfs, and some developers don't use the library printf, and there is no
way the compiler writers can anticiapte the syntaxes of those versions.

Now, even though it is not a gcc - specific question, would the Keeper of
the FAQ please add this or something like it?


		-- C^2

Looking for fine software and/or web pages?
http://web.idirect.com/~ccurley
-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

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

* Re: A TINY BUG
  1997-10-06  9:53 ` Jason Zions
                     ` (2 preceding siblings ...)
  1997-10-06 14:27   ` Marty Leisner
@ 1997-10-06 17:13   ` Steve Tynor
  1997-10-06 18:01   ` Chris Faylor
  4 siblings, 0 replies; 12+ messages in thread
From: Steve Tynor @ 1997-10-06 17:13 UTC (permalink / raw)
  To: Jason Zions; +Cc: tage.westlund, gnu-win32

Jason Zions wrote:

| > #include <stdio.h>
| > main(){
| >         printf("%s\n",sizeof(long));
....
| No compiler will detect this error at compile-time. The prototype for
....
| printf is (char *, ...); that is, no specific type information for
| anything except the first parameter. A compiler would have to read the
| first parameter to figure out the expected types for the remaining args,
....
| Summary: learn more about the language before whining about compiler
| errors. This is a programmer bug, not a compiler bug.

Before you flame, I suggest you read the gcc info page -- the great and
all powerful gcc _will_ detect this error -- at least is does on my
version of 2.7.2.1 on sparc-solaris.

Try using gcc -Wall (or -Wformat for just printf warnings).

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
In theory, there is no difference between theory and practice. 
But, in practice, there is.

Steve Tynor		Email:   Steve.Tynor@atlanta.twr.com
Tower Technology 	WWW:     http://www.twr.com/
Retooling the Software Industry for the 21st Century (sm)
-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

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

* Re: A TINY BUG
  1997-10-06  9:53 ` Jason Zions
                     ` (3 preceding siblings ...)
  1997-10-06 17:13   ` Steve Tynor
@ 1997-10-06 18:01   ` Chris Faylor
  4 siblings, 0 replies; 12+ messages in thread
From: Chris Faylor @ 1997-10-06 18:01 UTC (permalink / raw)
  To: gnu-win32

In article < 34391696.27758E19@softway.com >,
Jason Zions  <jazz@softway.com> wrote:
>> I have found that the following bad code gives "exception" at run
>> time instead of error message at compilation time (b18 Win95):
>> 
>> #include <stdio.h>
>> main(){
>>         printf("%s\n",sizeof(long));
>> }
>
>No compiler will detect this error at compile-time.  The prototype for
>printf is (char *, ...); that is, no specific type information for
>anything except the first parameter.  A compiler would have to read the
>first parameter to figure out the expected types for the remaining
>args, and much of the time that first parameter is dynamically computed
>at runtime instead of being a static string.  There are a couple of
>lint-like programs that will catch this error with a
>compile-time-evaluatable format string, but that's the best you can do.
>
>Summary: learn more about the language before whining about compiler
>errors.  This is a programmer bug, not a compiler bug.

Actually, this is a little harsh since GCC does, in fact, have a compile
time __attribute__ option for checking the arguments to a printf.  If
the prototype for printf in /usr/include/stdio.h had included something like:

    __attribute__((format(printf, 1, 2)));

it would have detected that programmer error.
-- 
http://www.bbc.com/	cgf@bbc.com			"Strange how unreal
VMS=>UNIX Solutions	Boston Business Computing	 the real can be."
-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

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

* A TINY BUG
  1997-10-06  1:19 A TINY BUG Tage Westlund
  1997-10-06  9:53 ` Jason Zions
  1997-10-06 15:47 ` Charles Curley
@ 1997-10-07  6:18 ` Jon Thackray
  2 siblings, 0 replies; 12+ messages in thread
From: Jon Thackray @ 1997-10-07  6:18 UTC (permalink / raw)
  To: gnu-win32

Tage Westlund writes:
 > To gnu designers!
 > I have found that the following bad code gives "exception" at run
 > time instead of error message at compilation time (b18 Win95):
 > 
 > #include <stdio.h>
 > main(){
 > 	printf("%s\n",sizeof(long));
 > }

C the language is not required to detect this error, and in the worst
case, simply can't. Consider

extern char *foo;
int main(int argc, char *argv[])
{
  printf(foo, sizeof(long));
  return 0;
}

The best a compiler could do would be to warn you that this might be
unsafe. OTOH, you may not want this behaviour, as you may believe that
programs should compile without errors or warnings, so that if errors
or warnigs are produced you know that you need to investigate them.
Many C compilers, at high warning levels, will detect the programmer
error you quote. But at the end of the day, with C, you are on your
own. C is not a safe language, and make no pretensions to be such. If
you wish to write in a safe language, perhaps you should try standard
ML.
-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

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

* RE: A TINY BUG
@ 1997-10-07  0:12 Pascal OBRY
  0 siblings, 0 replies; 12+ messages in thread
From: Pascal OBRY @ 1997-10-07  0:12 UTC (permalink / raw)
  To: jazz, tage.westlund; +Cc: gnu-win32

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1302 bytes --]

>> I have found that the following bad code gives "exception" at run
>> time instead of error message at compilation time (b18 Win95):
>>
>> #include <stdio.h>
>> main(){
>>         printf("%s\n",sizeof(long));
>> }

>Summary: learn more about the language before whining about compiler
>errors. This is a programmer bug, not a compiler bug.

What about : this is a language bug :-)

Pascal.

--|------------------------------------------------------------
--| Pascal Obry                               Team-Ada Member |
--|                                                           |
--| EDF-DER-IPN-SID- G A L A X I E                            |
--|                         web   : http://cln49ae            |
--| Bureau N-023            e-mail: pascal.obry@der.edfgdf.fr |
--| 1 Av Général de Gaulle  voice : +33-1-47.65.50.91         |
--| 92141 Clamart CEDEX     fax   : +33-1-47.65.50.07         |
--| FRANCE                                                    |
--|------------------------------------------------------------
--|
--|   http://ourworld.compuserve.com/homepages/pascal_obry
--|
--|   "The best way to travel is by means of imagination"
-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

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

* Re: A TINY BUG
  1997-10-06  9:16 David C. Hoos, Sr.
@ 1997-10-06 17:13 ` $Bill Luebkert
  0 siblings, 0 replies; 12+ messages in thread
From: $Bill Luebkert @ 1997-10-06 17:13 UTC (permalink / raw)
  To: David C. Hoos, Sr.; +Cc: tage.westlund, gnu-win32

David C. Hoos, Sr. wrote:
> 
> This is the kind of thing which compilers do not catch, because the problem
> is specific to
> the printf function which is a library unit, and not part of the language.
> In other words, as long as the first argument to printf is of type char *,
> then the function profile is matched.  If the string to which that first
> parameter points is not a valid printf format string matching the other
> pritf parameters, the compiler doesn't know it.  Furthermore, the string to
> which the first parameter of printf points can be (and often is) modified at
> run time, to fit the format to the occasion.
> 
> If you want to catch this sort of problem at build time, you need to use
> "lint", and it is only
> able to catch certain things because of the possibility of run-time
> modification.

Wrong, just add -Wall to your command line to pick up warnings and you
should get something like:
	 warning: format argument is not a pointer (arg 2)

> Better yet, write your program in ada95, using the free gnat-3.10p1 compiler
> from
> ftp://ftp.cs.nyu.edu/pub/gnat/winnt

Ada propaganda!  :)

> -----Original Message-----
> From: Tage Westlund <tage.westlund@stockholm.mail.telia.com>
> To: gnu-win32@cygnus.com <gnu-win32@cygnus.com>
> Date: Monday, October 06, 1997 4:38 AM
> Subject: A TINY BUG
> 
> >To gnu designers!
> >I have found that the following bad code gives "exception" at run
> >time instead of error message at compilation time (b18 Win95):
> >
> >#include <stdio.h>
> >main(){
> > printf("%s\n",sizeof(long));
> >}

-- 
  ,-/-  __      _  _         $Bill Luebkert
 (_/   /  )    // //       DBE Collectibles
  / ) /--<  o // //      http://www.wgn.net/~dbe/
-/-' /___/_<_</_</_    Email: dbe@wgn.net
-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

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

* Re: A TINY BUG
@ 1997-10-06  9:16 David C. Hoos, Sr.
  1997-10-06 17:13 ` $Bill Luebkert
  0 siblings, 1 reply; 12+ messages in thread
From: David C. Hoos, Sr. @ 1997-10-06  9:16 UTC (permalink / raw)
  To: tage.westlund, gnu-win32

This is the kind of thing which compilers do not catch, because the problem
is specific to
the printf function which is a library unit, and not part of the language.
In other words, as long as the first argument to printf is of type char *,
then the function profile is matched.  If the string to which that first
parameter points is not a valid printf format string matching the other
pritf parameters, the compiler doesn't know it.  Furthermore, the string to
which the first parameter of printf points can be (and often is) modified at
run time, to fit the format to the occasion.

If you want to catch this sort of problem at build time, you need to use
"lint", and it is only
able to catch certain things because of the possibility of run-time
modification.

Better yet, write your program in ada95, using the free gnat-3.10p1 compiler
from
ftp://ftp.cs.nyu.edu/pub/gnat/winnt

David C. Hoos, Sr.,
david.c.hoos.sr@ada95.com

-----Original Message-----
From: Tage Westlund <tage.westlund@stockholm.mail.telia.com>
To: gnu-win32@cygnus.com <gnu-win32@cygnus.com>
Date: Monday, October 06, 1997 4:38 AM
Subject: A TINY BUG


>To gnu designers!
>I have found that the following bad code gives "exception" at run
>time instead of error message at compilation time (b18 Win95):
>
>#include <stdio.h>
>main(){
> printf("%s\n",sizeof(long));
>}
>Tage
>
>-
>For help on using this list (especially unsubscribing), send a message to
>"gnu-win32-request@cygnus.com" with one line of text: "help".
>

-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

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

end of thread, other threads:[~1997-10-07  6:18 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-10-06  1:19 A TINY BUG Tage Westlund
1997-10-06  9:53 ` Jason Zions
1997-10-06 12:27   ` Steve Dum
1997-10-06 12:40   ` Matthew Moskewicz
1997-10-06 14:27   ` Marty Leisner
1997-10-06 17:13   ` Steve Tynor
1997-10-06 18:01   ` Chris Faylor
1997-10-06 15:47 ` Charles Curley
1997-10-07  6:18 ` Jon Thackray
1997-10-06  9:16 David C. Hoos, Sr.
1997-10-06 17:13 ` $Bill Luebkert
1997-10-07  0:12 Pascal OBRY

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