Bug 19310 - src/binutils/dwarf.c:6459: code relies on undefined behaviour ?
Summary: src/binutils/dwarf.c:6459: code relies on undefined behaviour ?
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: binutils (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-11-30 15:26 UTC by dcb
Modified: 2015-12-08 09:53 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description dcb 2015-11-30 15:26:07 UTC
[src/binutils/dwarf.c:6459]: (warning) Invalid test for overflow 'start+ul<start'. Condition is always false unless there is overflow, and overflow is UB.

     if (start >= block_end || start + ul > block_end || start + ul < start)
Comment 1 Nick Clifton 2015-12-04 12:53:06 UTC
Hmm, will this do the trick ?

-       if (start >= block_end || start + ul > block_end || start + ul < start)
+       if (start >= block_end || ul > (unsigned long) (block_end - start))

Cheers
  Nick
Comment 2 dcb 2015-12-04 16:55:35 UTC
>will this do the trick ?

It certainly seems to shut up cppcheck, the static analyser.

Not sure how to get gcc or clang to detect this problem.

$ cat dec4a.cc

extern void g( const char *);

void
f( const char * start, unsigned long ul)
{
	if (start + ul < start)
		g( start);
}

$ ~/gcc/results/bin/gcc -c -O2 -Wall -Wextra -Wtype-limits dec4a.cc
$ ~/llvm/results/bin/clang++ -c -O2 -Wall dec4a.cc
$
Comment 3 Sourceware Commits 2015-12-08 09:52:15 UTC
The master branch has been updated by Nick Clifton <nickc@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=7460c0abe8a195243110e66624da095a44cc96d6

commit 7460c0abe8a195243110e66624da095a44cc96d6
Author: Nick Clifton <nickc@redhat.com>
Date:   Tue Dec 8 09:49:49 2015 +0000

    Fix static analysis warning about undefined bheaviour.
    
    	PR binutils/19310
    	* dwarf.c (display_debug_frames): Recode range test to avoid
    	undefined behaviour.
Comment 4 Nick Clifton 2015-12-08 09:53:03 UTC
Patch applied