Warn about unspecified behaviour

  • Thread starter Tomás Ó hÉilidhe
  • Start date
T

Tomás Ó hÉilidhe

Is there any sort of tool out there that can warn about code that can
behave differently depending on "unspecified" behaviour? For instance,
take the following statement:

*p++ = ConvertToLowercase(*p);

In this statement, we don't know whether the lefthand or the righthand
side will be evaluated first. If the righthand side is first to be
evaluated, then we get:

*p = ConvertToLowercase(*p);
p = p + 1;

If the lefthand side is evaluated first, we get:

p = p + 1;
*p = ConvertToLowercase(*p);

The original statement has two possible distinct behaviours depending
on how the compiler chooses to handle the "unspecified" behaviour.

Is there any sort of tool that can go through code and warn about
instances in which "unspecified" behaviour can result in two or more
different behaviours?

gcc is neat in that it warns about a sequence point violation; for
instance if you do the following:
i++ + i++
then you get:
test.c:5: warning: operation on `i' may be undefined

Just wondering if there's a tool that will warn about unspecified
order of evaluation, or any unspecified behaviour for that matter.
 
3

31349 83652

Tomás Ó hÉilidhe said:
Just wondering if there's a tool that will warn about unspecified
order of evaluation, or any unspecified behaviour for that matter.

<OT>
gcc warns about that construct

tmp$ echo 'char f(char x){return x;}int main(void){char*p;*p+
+=f(*p);return 0;}' | /usr/bin/gcc -Wall -xc -
<stdin>: In function ‘main’:
<stdin>:1: warning: operation on ‘p’ may be undefined
tmp$


Or you could try lint.
</OT>
 
T

Tomás Ó hÉilidhe

gcc warns about that construct


Thanks for that. I must not have used -Wall when I was compiling my
code.

tmp$ echo 'char f(char x){return x;}int main(void){char*p;*p+
+=f(*p);return 0;}' | /usr/bin/gcc -Wall -xc -
<stdin>: In function ‘main’:
<stdin>:1: warning: operation on ‘p’ may be undefined
tmp$


That command line thing is pretty cool. Got it working on Windows just
there but I haven't figured out how to work it with preprocessor stuff.
 
C

CBFalconer

Tomás Ó hÉilidhe said:
Is there any sort of tool out there that can warn about code that
can behave differently depending on "unspecified" behaviour? For
instance, take the following statement:

*p++ = ConvertToLowercase(*p);

In this statement, we don't know whether the lefthand or the
righthand side will be evaluated first. If the righthand side is
first to be evaluated, then we get:

*p = ConvertToLowercase(*p);
p = p + 1;

If the lefthand side is evaluated first, we get:

p = p + 1;
*p = ConvertToLowercase(*p);

The original statement has two possible distinct behaviours
depending on how the compiler chooses to handle the "unspecified"
behaviour.

And neither may occur. The standard clearly states that the
results are undefined. If you write foolish code like that you
expect random crashes.

Some tools will warn about some failures. None will catch all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top