From 35050abeb8216f9c3fab8de87b03431b1ffba20d Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Tue, 27 Jun 2023 17:19:36 -0400 Subject: [PATCH 86/98] FIXME: add test: memccpy-1.c --- .../c-c++-common/analyzer/memccpy-1.c | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 gcc/testsuite/c-c++-common/analyzer/memccpy-1.c diff --git a/gcc/testsuite/c-c++-common/analyzer/memccpy-1.c b/gcc/testsuite/c-c++-common/analyzer/memccpy-1.c new file mode 100644 index 000000000000..dc06987ca417 --- /dev/null +++ b/gcc/testsuite/c-c++-common/analyzer/memccpy-1.c @@ -0,0 +1,55 @@ +/* See e.g. https://en.cppreference.com/w/c/string/byte/memccpy + and https://man7.org/linux/man-pages/man3/memccpy.3p.html */ + +extern void *memccpy(void *dest, const void *src, int c, size_t count); + +void * +test_passthrough (void *dest, const void *src, int c, size_t count) +{ + return memccpy(dest, src, c, count); +} + +void * +test_null_dest (const void *src, int c, size_t count) +{ + return memccpy(NULL, src, c, count); // FIXME: complain about this? +} + +void * +test_null_src (void *dest, int c, size_t count) +{ + return memccpy(dest, NULL, c, count); // FIXME: complain about this? +} + +/* TODO: + - ptrs not at start of string + - count == 0 + +"Copies bytes from the object pointed to by src to the object pointed to by dest, stopping after any of the next two conditions are satisfied: + + count bytes are copied + the byte (unsigned char)c is found (and copied). + +The src and dest objects are interpreted as arrays of unsigned char. + +The behavior is undefined if any condition is met: + + access occurs beyond the end of the dest array; + the objects overlap (which is a violation of the restrict contract) + either dest or src is an invalid or null pointer + +Parameters +dest - pointer to the object to copy to +src - pointer to the object to copy from +c - terminating byte, converted to unsigned char at first +count - number of bytes to copy +Return value + +If the byte (unsigned char)c was found memccpy returns a pointer to the next byte in dest after (unsigned char)c, otherwise returns null pointer. +Notes + +The function is identical to the POSIX memccpy. + +memccpy(dest, src, 0, count) behaves similar to strncpy(dest, src, count), except that the former returns a pointer to the end of the buffer written, and does not zero-pad the destination array. Thus, memccpy is useful for efficiently concatenating multiple strings. " + + */ -- 2.49.0