Skip to content

Commit 634e4ed

Browse files
committed
Fix #15172 FP constParameterPointer with mmap() addr argument
The first argument to mmap() was marked as direction="in", causing constParameterPointer to suggest declaring it as pointer to const. However, with MAP_FIXED any existing mapping at addr is replaced, so the pointed-to memory is effectively modified. Drop the direction from the first argument of mmap() in bsd.cfg and posix.cfg, and mmap64() in posix.cfg.
1 parent 0c4dc89 commit 634e4ed

6 files changed

Lines changed: 25 additions & 4 deletions

File tree

‎cfg/bsd.cfg‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@
443443
<returnValue type="void *"/>
444444
<noreturn>false</noreturn>
445445
<leak-ignore/>
446-
<arg nr="1" direction="in">
446+
<arg nr="1">
447447
<not-uninit/>
448448
</arg>
449449
<arg nr="2" direction="in">

‎cfg/gnu.cfg‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1991,7 +1991,7 @@
19911991
<returnValue type="void *"/>
19921992
<noreturn>false</noreturn>
19931993
<leak-ignore/>
1994-
<arg nr="1" direction="in">
1994+
<arg nr="1">
19951995
<not-uninit/>
19961996
</arg>
19971997
<arg nr="2" direction="in">

‎cfg/posix.cfg‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2472,7 +2472,7 @@ The function 'mktemp' is considered to be dangerous due to race conditions and s
24722472
<returnValue type="void *"/>
24732473
<noreturn>false</noreturn>
24742474
<leak-ignore/>
2475-
<arg nr="1" direction="in">
2475+
<arg nr="1">
24762476
<not-uninit/>
24772477
</arg>
24782478
<arg nr="2" direction="in">
@@ -2504,7 +2504,7 @@ The function 'mktemp' is considered to be dangerous due to race conditions and s
25042504
<returnValue type="void *"/>
25052505
<noreturn>false</noreturn>
25062506
<leak-ignore/>
2507-
<arg nr="1" direction="in">
2507+
<arg nr="1">
25082508
<not-uninit/>
25092509
</arg>
25102510
<arg nr="2" direction="in">

‎test/cfg/bsd.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <string.h>
1212
#include <stdlib.h>
1313
#include <stdint.h>
14+
#include <sys/mman.h>
1415
#include <sys/time.h>
1516
#include <sys/uio.h>
1617

@@ -180,3 +181,9 @@ void reallocarray_notused(void)
180181
// cppcheck-suppress [leakReturnValNotUsed, ignoredReturnValue]
181182
reallocarray(NULL, 10, 10);
182183
}
184+
185+
void mmap_fixed(void *addr) // No constParameterPointer: mmap() may replace the mapping at addr
186+
{
187+
// cppcheck-suppress valueFlowBailoutIncompleteVar
188+
(void)mmap(addr, 0x1000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0);
189+
}

‎test/cfg/gnu.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,3 +905,10 @@ int nullPointer_epoll_ctl(int epfd, int op, int fd, struct epoll_event *event)
905905
return epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL);
906906
}
907907
#endif
908+
909+
void mmap_fixed(void *addr) // No constParameterPointer: mmap() may replace the mapping at addr
910+
{
911+
// cppcheck-suppress valueFlowBailoutIncompleteVar
912+
// cppcheck-suppress leakReturnValNotUsed
913+
(void)mmap(addr, 0x1000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
914+
}

‎test/cfg/posix.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,3 +1628,10 @@ void pthread_define_test()
16281628
PTHREAD_SCOPE_PROCESS;
16291629
PTHREAD_SCOPE_SYSTEM;
16301630
}
1631+
1632+
void mmap_fixed(void *addr) // No constParameterPointer: mmap() may replace the mapping at addr
1633+
{
1634+
// cppcheck-suppress valueFlowBailoutIncompleteVar
1635+
// cppcheck-suppress leakReturnValNotUsed
1636+
(void)mmap(addr, 0x1000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED, 0, 0);
1637+
}

0 commit comments

Comments
 (0)