Scary UB 
Author Message
 Scary UB

Is this really invoking UB?!? And can you spot it?

int catanddog(void)
{
   puts("Miauing and barking\n");

Quote:
}

--
Thomas.

Approaching singularity.



Fri, 04 Feb 2005 08:04:34 GMT  
 Scary UB
Quote:

> Is this really invoking UB?!? And can you spot it?

> int catanddog(void)
> {
>    puts("Miauing and barking\n");
> }

No value is being returned from a function defined to return `int'.

Everything else is fine.

HTH,
--ag

--
Artie Gold  --  Austin, TX

"May you keep turning the pages. And may the book never end."



Fri, 04 Feb 2005 09:11:08 GMT  
 Scary UB
On Mon, 19 Aug 2002 02:04:34 +0200, "Thomas Stegen CES2000"

Quote:
> Is this really invoking UB?!? And can you spot it?

> int catanddog(void)
> {
>    puts("Miauing and barking\n");
> }

According to the standard, this does not in itself invoke undefined
behavior.  If the calling function attempts to make any use of the
function's return value in any way, such as assigning it to a
variable, that invokes undefined behavior.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq



Fri, 04 Feb 2005 11:00:51 GMT  
 Scary UB


Quote:
>Is this really invoking UB?!? And can you spot it?
>int catanddog(void) { puts("Miauing and barking\n"); }

Missing return value => UB only if caller tries to use it.  Name
"catand" possibly reserved for double-precision complex arc-tangent
function, except that C99 gives you 31 -- not six -- significant
characters in function names, and calls double-precision complex
arctan "catan"; and C89 did not have complex types. :-)
--
In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)





Fri, 04 Feb 2005 12:04:37 GMT  
 Scary UB



Quote:
> Is this really invoking UB?!?

Yes.

Quote:
> And can you spot it?

Invocation of library function ('puts()') with no
prototype in scope.

There's also a semantic error: return type of the
function is declared as 'int', but no value is
returned.

-Mike

Quote:

> int catanddog(void)
> {
>    puts("Miauing and barking\n");
> }

> --
> Thomas.

> Approaching singularity.



Fri, 04 Feb 2005 16:32:47 GMT  
 Scary UB


Quote:

> > Is this really invoking UB?!? And can you spot it?

> > int catanddog(void)
> > {
> >    puts("Miauing and barking\n");
> > }

> No value is being returned from a function defined to return `int'.

> Everything else is fine.

Not quite.  There's no prototype for 'puts()' in scope.

-Mike



Fri, 04 Feb 2005 16:33:31 GMT  
 Scary UB

Quote:

> Invocation of library function ('puts()') with no
> prototype in scope.

That is UB in C99, but in C89 it is assumed to be declared:
  int puts();
and by chance that declaration is valid for puts.

Or is there a specific clause requiring prototypes for calls to library
functions in C89?

--
Simon.



Fri, 04 Feb 2005 18:16:36 GMT  
 Scary UB

Quote:


vertically]:
> >Is this really invoking UB?!? And can you spot it?
> >int catanddog(void) { puts("Miauing and barking\n"); }

> Missing return value => UB only if caller tries to use it.  Name
> "catand" possibly reserved for double-precision complex arc-tangent
> function, except that C99 gives you 31 -- not six -- significant
> characters in function names, and calls double-precision complex
> arctan "catan"; and C89 did not have complex types. :-)

This was the one I was looking for. I was a bit quick with my example.
Perhaps

void printform(char **form)
{
   /* print form somehow */

Quote:
}

would have been a better example.

--
Thomas.

Approaching singularity.



Fri, 04 Feb 2005 17:27:40 GMT  
 Scary UB


Quote:




> vertically]:
> > >Is this really invoking UB?!? And can you spot it?
> > >int catanddog(void) { puts("Miauing and barking\n"); }

> > Missing return value => UB only if caller tries to use it.  Name
> > "catand" possibly reserved for double-precision complex arc-tangent
> > function, except that C99 gives you 31 -- not six -- significant
> > characters in function names, and calls double-precision complex
> > arctan "catan"; and C89 did not have complex types. :-)

> This was the one I was looking for. I was a bit quick with my example.
> Perhaps

> void printform(char **form)
> {
>    /* print form somehow */
> }

> would have been a better example.

Or even more subtly: printForm.

But there are lots of (technically) potential identifier traps: isotope,
EMPTY, strong, top, setbuffer, PRINT, toggle, memory, SIGN, E1..E8,
remove_node, INTERNAL_MAX...

--
Peter



Fri, 04 Feb 2005 19:37:19 GMT  
 Scary UB

Quote:




> > > Is this really invoking UB?!? And can you spot it?

> > > int catanddog(void)
> > > {
> > >    puts("Miauing and barking\n");
> > > }

> > No value is being returned from a function defined to return `int'.

> > Everything else is fine.

> Not quite.  There's no prototype for 'puts()' in scope.

Therefore, the compiler is entitled to assume it returns int, which it
does. Since it's not a variadic function, the worst that happens is that
you lose a bit of typechecking, but that's okay because he's used the
correct type for the argument to puts().

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Fri, 04 Feb 2005 19:31:25 GMT  
 Scary UB

Quote:
>    puts("Miauing and barking\n");

In addition to other answers, this function call results in *two*
end-of-lines. (puts() provides a '\n')

If you were expecting just the one, this is an error in the code. It's
not undefined behaviour though.

The missing puts prototype and return have already been mentioned by
others.

Bill, twit two wu.



Fri, 04 Feb 2005 21:12:58 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. This is scary

2. Top 10 scariest posters to comp.lang.c

3. Want to hear something scary?

4. Now *this* is scary

5. You be (UB) or not you be

6. clrscr and UB

7. reverse string - UB

8. Converting unsigned char to signed char invokes UB?

9. String literals and UB

10. still UB?

11. UB, or a stupid mistake, or both?

 

 
Powered by phpBB® Forum Software