Bug 217881

Summary: devel/cppcheck: version 1.77 always crashes on amd64
Product: Ports & Packages Reporter: Enji Cooper <ngie>
Component: Individual Port(s)Assignee: Dmitry Marakasov <amdmi3>
Status: Closed FIXED    
Severity: Affects Some People CC: monwarez, ngie, philippe.michel7, rozhuk.im, w.schwarzenfeld
Priority: --- Flags: bugzilla: maintainer-feedback? (amdmi3)
Version: Latest   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
gdb backtrace + end of truss output
none
std.cfg none

Description Enji Cooper freebsd_committer freebsd_triage 2017-03-17 21:54:15 UTC
$ cat ~/calloc_test.c
#include <stdlib.h>
#include <stdio.h>

int
main(void)
{
        void *memory;

        memory = calloc(1, 0);
        printf("memory is NULL: %d\n", memory == NULL);
        free(memory);

        memory = calloc(0, 1);
        printf("memory is NULL: %d\n", memory == NULL);
        free(memory);

        return (0);
}
$ cppcheck ~/calloc_test.c
Segmentation fault (core dumped)
$
Comment 1 Enji Cooper freebsd_committer freebsd_triage 2017-03-17 21:55:13 UTC
(In reply to Ngie Cooper from comment #0)
$ uname -a
FreeBSD wkstn-fbsd-ngie 12.0-CURRENT FreeBSD 12.0-CURRENT #35 r312933+593b97e50212(isilon-atf)-dirty: Sat Jan 28 15:44:49 PST 2017     ngie@wkstn-fbsd-ngie:/usr/obj/usr/src/sys/GENERIC-NODEBUG  amd64

I wonder if it's using basename/dirname in the non-POSIX compliant way, which was "broken" recently in favor of POSIX compliance.
Comment 2 Philippe Michel 2017-03-20 21:46:21 UTC
Created attachment 181017 [details]
gdb backtrace + end of truss output
Comment 3 Philippe Michel 2017-03-20 21:50:20 UTC
On 11.0-STABLE r315434 amd64, a cppcheck binary that had worked on Jan 11 now fails with :

elf_load_section: truncated ELF file
Abort

After rebuilding it, it fails with :

Bus error (core dumped)
Comment 4 Dmitry Marakasov freebsd_committer freebsd_triage 2017-03-27 13:12:03 UTC
I cannot reproduce this problem on r315909 amd64.
Comment 5 Walter Schwarzenfeld 2017-03-27 14:31:59 UTC
Maybe =>
open("std.cfg",O_RDONLY,0666)                    ERR#2 'No such file or directory'
open("/usr/local/share/cppcheck/cfg/std.cfg",O_RDONLY,0666) = 3 (0x3)

but I get an other error message if I rename std.cfg.
Comment 6 Dmitry Marakasov freebsd_committer freebsd_triage 2017-03-28 13:24:52 UTC
(In reply to w.schwarzenfeld from comment #5)

> Maybe =>
> open("std.cfg",O_RDONLY,0666)                    ERR#2 'No such file or
> directory'
> open("/usr/local/share/cppcheck/cfg/std.cfg",O_RDONLY,0666) = 3 (0x3)
> 
> but I get an other error message if I rename std.cfg.

I didn't quite get that.
Comment 7 Walter Schwarzenfeld 2017-03-28 13:29:51 UTC
Created attachment 181266 [details]
std.cfg

There is the std.cfg from my system (it is 10.3 but this should no difference).
Comment 8 Walter Schwarzenfeld 2017-03-28 13:33:10 UTC
Another way is tho extract devel/cppcheck in the port and copy it from the source.
Comment 9 Dmitry Marakasov freebsd_committer freebsd_triage 2017-03-28 15:05:04 UTC
Please list all steps to reproduce the problem in detail.
Comment 10 Thibault Payet 2017-04-04 14:32:38 UTC
At first I was getting a SIGSEGV , but after multiple rebuild with sysutils/synth , and at the end adding WITH_DEBUG=yes. I did not get the SIGSEGV anymore.
Comment 11 Thibault Payet 2017-04-04 14:34:03 UTC
(In reply to Thibault Payet from comment #10)
At the end, I was using 1.77_1
Comment 12 Dmitry Marakasov freebsd_committer freebsd_triage 2017-04-13 12:42:56 UTC
Feedback timeout, unable to reproduce. I've tested it on different current installations and it works fine.
Comment 13 Enji Cooper freebsd_committer freebsd_triage 2017-05-03 16:38:54 UTC
Still happens for me:

$ !439
cppcheck bin/dd/dd.c 
Segmentation fault (core dumped)
$ uname -a
FreeBSD fbsd12 12.0-CURRENT FreeBSD 12.0-CURRENT #13 r317727: Tue May  2 17:22:45 PDT 2017     ngie@fbsd12:/usr/obj/usr/src/sys/GENERIC-NODEBUG  amd64

Have you compiled libc with MALLOC_PRODUCTION? Mine has all of the debugging stuff turned on. Let's try with MALLOC_PRODUCTION enabled...

(set -e; export MK_TESTS=no MAKEFLAGS="-DMALLOC_PRODUCTION -j4"; cd lib/libc; make clean; make; sudo -E make install)

$ cppcheck bin/dd/dd.c 
Segmentation fault (core dumped)

Nope. I don't know what's special about my setup, but it definitely repros this issue 100% of the time.

I'll look at the cppcheck source to see if I can spot something obvious that my set of build options might be triggering (and the upstream maintainer didn't add error checking for...).
Comment 14 Philippe Michel 2017-05-16 00:05:05 UTC
I still have the problem (with 1.78_2 now) with the default options.

On the other hand, commenting out USE_CXXSTD=c++11 in the port Makefile builds a working executable.

FreeBSD 11.0-STABLE r317611 amd64
FreeBSD clang version 4.0.0 (tags/RELEASE_400/final 297347)
Comment 15 Dmitry Marakasov freebsd_committer freebsd_triage 2017-05-19 16:27:51 UTC
Finally able to reproduce (11-stable). Seem to be a problem in either libxml2 or newer clang:

#0  tinyxml2::MemPoolT<104>::Alloc (this=0x7fffffffdf00) at tinyxml2.h:368
#1  0x00000000005dfe84 in tinyxml2::XMLDocument::Identify (this=0x7fffffffdcc0, p=<value optimized out>, node=<value optimized out>) at externals/tinyxml/tinyxml2.cpp:669
#2  0x00000000005e0a21 in tinyxml2::XMLNode::ParseDeep (this=0x7fffffffdcc0, p=0x0, parentEnd=0x0, curLineNumPtr=0x7fffffffdd78) at externals/tinyxml/tinyxml2.cpp:979
#3  0x00000000005e48fb in tinyxml2::XMLDocument::LoadFile (this=0x7fffffffdcc0, fp=0x801954f30) at externals/tinyxml/tinyxml2.cpp:2167
#4  0x00000000005e4825 in tinyxml2::XMLDocument::LoadFile (this=0x7fffffffdcc0, filename=0x801e41000 "/wrkdirs/usr/ports/devel/cppcheck/work/stage/usr/local/share/cppcheck/cfg/std.cfg") at externals/tinyxml/tinyxml2.cpp:2098
#5  0x00000000005188ee in Library::load (this=<value optimized out>, exename=<value optimized out>, path=<value optimized out>) at lib/library.cpp:93
#6  0x00000000004563a9 in CppCheckExecutor::tryLoadLibrary (destination=<value optimized out>, basepath=<value optimized out>, filename=0x5ebd47 "std.cfg") at cli/cppcheckexecutor.cpp:988
#7  0x0000000000454da8 in CppCheckExecutor::check_internal (this=0x7fffffffea08, cppcheck=@0x7fffffffe308, argv=0x7fffffffead8) at cli/cppcheckexecutor.cpp:773
#8  0x0000000000454bf1 in CppCheckExecutor::check (this=0x7fffffffea08, argc=2, argv=0x7fffffffead8) at cli/cppcheckexecutor.cpp:186
#9  0x000000000045853b in main (argc=<value optimized out>, argv=<value optimized out>) at cli/main.cpp:136
Comment 16 Dmitry Marakasov freebsd_committer freebsd_triage 2017-05-19 16:27:58 UTC
*** Bug 218876 has been marked as a duplicate of this bug. ***
Comment 17 Ivan Rozhuk 2017-05-24 12:39:47 UTC
1. cppcheck does not depend from libtinyxml2.so:textproc/tinyxml2 it use build in from: externals/tinyxml

2. If I update by hands externals/tinyxml to tinyxml files form git up to 4.0.1 and build then cppcheck does not crash. (I have updated tinyxml2 in system, patch to bug tracker will done a bit later)

3. TINYXML=-ltinyxml2 - does not used at all.

4. post-patch:
        @${REINPLACE_CMD} \
        -e 's|-Iexternals/tinyxml|-I${LOCALBASE}/include|' \
                ${WRKSRC}/Makefile
Makes cppcheck to fail.
Because you use .h from more older tinyxml from system with more newer .cpp from externals/tinyxml.

So, please remove patch and depends for textproc/tinyxml2 or do proper patch to use it instead of bundled externals/tinyxml.
Comment 18 Ivan Rozhuk 2017-05-24 20:20:16 UTC
tinyxml2 update to 4.0.1
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=219510
Comment 19 commit-hook freebsd_committer freebsd_triage 2017-05-31 20:08:00 UTC
A commit references this bug:

Author: amdmi3
Date: Wed May 31 20:07:16 UTC 2017
New revision: 442261
URL: https://svnweb.freebsd.org/changeset/ports/442261

Log:
  - Update to 1.79
  - Fix segfault due to conflict between bundled and system tinyxml [1]
  - Switch to cmake build, simplifying the port
  - Enable GUI

  PR:		217881 [1], 218876 [1]

Changes:
  head/devel/cppcheck/Makefile
  head/devel/cppcheck/distinfo
  head/devel/cppcheck/files/
  head/devel/cppcheck/files/patch-CMakeLists.txt
  head/devel/cppcheck/files/patch-gui_CMakeLists.txt
  head/devel/cppcheck/files/patch-tools_matchcompiler.py
  head/devel/cppcheck/pkg-plist
Comment 20 Enji Cooper freebsd_committer freebsd_triage 2017-06-06 15:56:12 UTC
It works now...

$ cppcheck bin/test/test.c 
Checking bin/test/test.c ...
Checking bin/test/test.c: SHELL...

P.S. Enabling x11 on cppcheck adds a lot of bloatware to my non-x11 VM.
Comment 21 Dmitry Marakasov freebsd_committer freebsd_triage 2017-06-30 18:15:34 UTC
(In reply to Ngie Cooper from comment #20)

> P.S. Enabling x11 on cppcheck adds a lot of bloatware to my non-x11 VM.

I'm working on a separate cppcheck-gui port
Comment 22 Dmitry Marakasov freebsd_committer freebsd_triage 2017-07-10 17:42:01 UTC
(In reply to Dmitry Marakasov from comment #21)

> I'm working on a separate cppcheck-gui port

Done.