@@ -2316,3 +2316,153 @@ def test_builddir_hash_check_level(tmp_path): # #13376
23162316 hash_2 = root .get ('hash' )
23172317
23182318 assert hash_1 != hash_2
2319+
2320+
2321+ def test_def_undef (tmp_path ):
2322+ test_file = tmp_path / 'test.c'
2323+ with open (test_file , 'wt' ) as f :
2324+ f .write ("""
2325+ void f()
2326+ {
2327+ #ifndef DEF_1
2328+ {int i = *((int*)0);}
2329+ #endif
2330+ }
2331+ """ )
2332+
2333+ args = [
2334+ '--template=simple' ,
2335+ '-DDEF_1' ,
2336+ '-UDEF_1' ,
2337+ str (test_file )
2338+ ]
2339+ exitcode , stdout , stderr = cppcheck (args )
2340+ assert exitcode == 0
2341+ assert stdout .splitlines () == [
2342+ 'Checking {} ...' .format (test_file ),
2343+ 'Checking {}: DEF_1=1...' .format (test_file ) # TODO: should not print DEF_1 - see #13335
2344+ ]
2345+ assert stderr .splitlines () == [
2346+ '{}:5:16: error: Null pointer dereference: (int*)0 [nullPointer]' .format (test_file )
2347+ ]
2348+
2349+
2350+ @pytest .mark .xfail (strict = True )
2351+ def test_def_def (tmp_path ): # #13334
2352+ test_file = tmp_path / 'test.c'
2353+ with open (test_file , 'wt' ) as f :
2354+ f .write ("""
2355+ void f()
2356+ {
2357+ #if DEF_1 == 3
2358+ {int i = *((int*)0);}
2359+ #endif
2360+ #if DEF_1 == 7
2361+ {int i = *((int*)0);}
2362+ #endif
2363+ }
2364+ """ )
2365+
2366+ args = [
2367+ '--template=simple' ,
2368+ '-DDEF_1=3' ,
2369+ '-DDEF_1=7' ,
2370+ str (test_file )
2371+ ]
2372+ exitcode , stdout , stderr = cppcheck (args )
2373+ assert exitcode == 0
2374+ assert stdout .splitlines () == [
2375+ 'Checking {} ...' .format (test_file ),
2376+ 'Checking {}: DEF_1=3;DEF_1=7...' .format (test_file ) # TODO: should not print DEF_1 twice - see #13335
2377+ ]
2378+ assert stderr .splitlines () == [
2379+ '{}:8:16: error: Null pointer dereference: (int*)0 [nullPointer]' .format (test_file )
2380+ ]
2381+
2382+
2383+ @pytest .mark .xfail (strict = True )
2384+ def test_def_undef_def (tmp_path ): # #13334
2385+ test_file = tmp_path / 'test.c'
2386+ with open (test_file , 'wt' ) as f :
2387+ f .write ("""
2388+ void f()
2389+ {
2390+ #ifdef DEF_1
2391+ {int i = *((int*)0);}
2392+ #endif
2393+ }
2394+ """ )
2395+
2396+ args = [
2397+ '--template=simple' ,
2398+ '-DDEF_1' ,
2399+ '-UDEF_1' ,
2400+ '-DDEF_1' ,
2401+ str (test_file )
2402+ ]
2403+ exitcode , stdout , stderr = cppcheck (args )
2404+ assert exitcode == 0
2405+ assert stdout .splitlines () == [
2406+ 'Checking {} ...' .format (test_file ),
2407+ 'Checking {}: DEF_1=1;DEF_1=1...' .format (test_file ) # TODO: should not print DEF_1 twice - see #13335
2408+ ]
2409+ assert stderr .splitlines () == [
2410+ '{}:5:16: error: Null pointer dereference: (int*)0 [nullPointer]' .format (test_file )
2411+ ]
2412+
2413+
2414+ def test_undef (tmp_path ):
2415+ test_file = tmp_path / 'test.c'
2416+ with open (test_file , 'wt' ) as f :
2417+ f .write ("""
2418+ void f()
2419+ {
2420+ #ifndef DEF_1
2421+ {int i = *((int*)0);}
2422+ #endif
2423+ }
2424+ """ )
2425+
2426+ args = [
2427+ '--template=simple' ,
2428+ '-UDEF_1' ,
2429+ str (test_file )
2430+ ]
2431+ exitcode , stdout , stderr = cppcheck (args )
2432+ assert exitcode == 0
2433+ assert stdout .splitlines () == [
2434+ 'Checking {} ...' .format (test_file )
2435+ ]
2436+ assert stderr .splitlines () == [
2437+ '{}:5:16: error: Null pointer dereference: (int*)0 [nullPointer]' .format (test_file )
2438+ ]
2439+
2440+
2441+ @pytest .mark .xfail (strict = True )
2442+ def test_undef_src (tmp_path ): # #13340
2443+ test_file = tmp_path / 'test.c'
2444+ with open (test_file , 'wt' ) as f :
2445+ f .write ("""
2446+ #define DEF_1
2447+
2448+ void f()
2449+ {
2450+ #ifdef DEF_1
2451+ {int i = *((int*)0);}
2452+ #endif
2453+ }
2454+ """ )
2455+
2456+ args = [
2457+ '--template=simple' ,
2458+ '-UDEF_1' ,
2459+ str (test_file )
2460+ ]
2461+ exitcode , stdout , stderr = cppcheck (args )
2462+ assert exitcode == 0
2463+ assert stdout .splitlines () == [
2464+ 'Checking {} ...' .format (test_file )
2465+ ]
2466+ assert stderr .splitlines () == [
2467+ '{}:7:16: error: Null pointer dereference: (int*)0 [nullPointer]' .format (test_file )
2468+ ]
0 commit comments