mirror of
https://github.com/Zenithsiz/ftmemsim-valgrind.git
synced 2026-02-03 01:51:29 +00:00
Add an intercept for stpncpy. Fixes #309427.
(Mark Wielaard, mjw@redhat.com) git-svn-id: svn://svn.valgrind.org/valgrind/trunk@13163
This commit is contained in:
parent
8904839e87
commit
5ed1bb6a2f
@ -101,6 +101,7 @@
|
||||
20390 WCSCPY
|
||||
20400 WCSCHR
|
||||
20410 WCSRCHR
|
||||
20420 STPNCPY
|
||||
*/
|
||||
|
||||
|
||||
@ -983,6 +984,34 @@ static inline void my_exit ( int x )
|
||||
#endif
|
||||
|
||||
|
||||
/*---------------------- stpncpy ----------------------*/
|
||||
|
||||
#define STPNCPY(soname, fnname) \
|
||||
char* VG_REPLACE_FUNCTION_EZU(20420,soname,fnname) \
|
||||
( char* dst, const char* src, SizeT n ); \
|
||||
char* VG_REPLACE_FUNCTION_EZU(20420,soname,fnname) \
|
||||
( char* dst, const char* src, SizeT n ) \
|
||||
{ \
|
||||
const HChar* src_orig = src; \
|
||||
HChar* dst_str = dst; \
|
||||
SizeT m = 0; \
|
||||
\
|
||||
while (m < n && *src) { m++; *dst++ = *src++; } \
|
||||
/* Check for overlap after copying; all n bytes of dst are relevant, */ \
|
||||
/* but only m+1 bytes of src if terminator was found */ \
|
||||
if (is_overlap(dst_str, src_orig, n, (m < n) ? m+1 : n)) \
|
||||
RECORD_OVERLAP_ERROR("stpncpy", dst, src, n); \
|
||||
dst_str = dst; \
|
||||
while (m++ < n) *dst++ = 0; /* must pad remainder with nulls */ \
|
||||
\
|
||||
return dst_str; \
|
||||
}
|
||||
|
||||
#if defined(VGO_linux)
|
||||
STPNCPY(VG_Z_LIBC_SONAME, stpncpy)
|
||||
#endif
|
||||
|
||||
|
||||
/*---------------------- memset ----------------------*/
|
||||
|
||||
/* Why are we bothering to intercept this? It seems entirely
|
||||
|
||||
@ -190,6 +190,7 @@ EXTRA_DIST = \
|
||||
signal2.stderr.exp signal2.stdout.exp signal2.vgtest \
|
||||
sigprocmask.stderr.exp sigprocmask.stderr.exp2 sigprocmask.vgtest \
|
||||
static_malloc.stderr.exp static_malloc.vgtest \
|
||||
stpncpy.vgtest stpncpy.stderr.exp stpncpy.stdout.exp \
|
||||
strchr.stderr.exp strchr.stderr.exp2 strchr.stderr.exp-darwin \
|
||||
strchr.stderr.exp3 strchr.vgtest \
|
||||
str_tester.stderr.exp str_tester.vgtest \
|
||||
@ -286,6 +287,7 @@ check_PROGRAMS = \
|
||||
sbfragment \
|
||||
sh-mem sh-mem-random \
|
||||
sigaltstack signal2 sigprocmask static_malloc sigkill \
|
||||
stpncpy \
|
||||
strchr \
|
||||
str_tester \
|
||||
supp_unknown supp1 supp2 suppfree \
|
||||
|
||||
37
memcheck/tests/stpncpy.c
Normal file
37
memcheck/tests/stpncpy.c
Normal file
@ -0,0 +1,37 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
char a[] = "The spazzy orange tiger jumped over the tawny jaguar.";
|
||||
char *b, *c;
|
||||
char *d, *e;
|
||||
|
||||
size_t l = strlen (a);
|
||||
fprintf (stderr, "strlen: %zd\n", l); // strlen: 53
|
||||
|
||||
b = (char *) malloc((l + 3)); // Extra space for some zeros.
|
||||
b[l] = 'X';
|
||||
b[l + 1] = 'X';
|
||||
b[l + 2] = 'X';
|
||||
c = stpncpy (b, a, l + 3);
|
||||
|
||||
fprintf (stderr, "equal: %d\n", strcmp (a, b)); // equal: 0
|
||||
fprintf (stderr, "retlen: %zd\n", c - b); // retlen: 53
|
||||
fprintf (stderr, "last: '%c'\n", *(c - 1)); // last: '.'
|
||||
fprintf (stderr, "zero0: %d\n", *c); // zero0: 0
|
||||
fprintf (stderr, "zero1: %d\n", *(c + 1)); // zero1: 0
|
||||
fprintf (stderr, "zero2: %d\n", *(c + 2)); // zero2: 0
|
||||
|
||||
d = (char *) malloc (l - 1); // No room for zero termination or dot.
|
||||
e = stpncpy (d, b, l - 1);
|
||||
|
||||
fprintf (stderr, "equal: %d\n", strncmp (b, d, l - 1)); // equal: 0
|
||||
fprintf (stderr, "retlen: %zd\n", e - d); // retlen: 52
|
||||
fprintf (stderr, "last: '%c'\n", *(e - 1)); // last: 'r'
|
||||
|
||||
free (b);
|
||||
free (d);
|
||||
return 0;
|
||||
}
|
||||
10
memcheck/tests/stpncpy.stderr.exp
Normal file
10
memcheck/tests/stpncpy.stderr.exp
Normal file
@ -0,0 +1,10 @@
|
||||
strlen: 53
|
||||
equal: 0
|
||||
retlen: 53
|
||||
last: '.'
|
||||
zero0: 0
|
||||
zero1: 0
|
||||
zero2: 0
|
||||
equal: 0
|
||||
retlen: 52
|
||||
last: 'r'
|
||||
0
memcheck/tests/stpncpy.stdout.exp
Normal file
0
memcheck/tests/stpncpy.stdout.exp
Normal file
2
memcheck/tests/stpncpy.vgtest
Normal file
2
memcheck/tests/stpncpy.vgtest
Normal file
@ -0,0 +1,2 @@
|
||||
prog: stpncpy
|
||||
vgopts: -q
|
||||
Loading…
x
Reference in New Issue
Block a user