Path: blob/main/lib/libc/tests/string/stpncpy_test.c
104419 views
/*-1* Copyright (c) 2009 David Schultz <[email protected]>2* Copyright (c) 2023, 2025 The FreeBSD Foundation3* All rights reserved.4*5* Portions of this software were developed by Robert Clausecker6* <[email protected]> under sponsorship from the FreeBSD Foundation.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND18* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE21* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL22* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS23* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)24* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT25* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY26* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF27* SUCH DAMAGE.28*/2930#include <sys/param.h>31#include <sys/mman.h>32#include <assert.h>33#include <dlfcn.h>34#include <stdio.h>35#include <stdlib.h>36#include <string.h>3738#include <atf-c.h>3940static char *(*stpncpy_fn)(char *restrict, const char *restrict, size_t);4142static char *43makebuf(size_t len, int guard_at_end)44{45char *buf;46size_t alloc_size, page_size;4748page_size = getpagesize();49alloc_size = roundup2(len, page_size) + page_size;5051buf = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);52assert(buf);53if (guard_at_end) {54assert(mprotect(buf + alloc_size - page_size, page_size, PROT_NONE) == 0);55return (buf + alloc_size - page_size - len);56} else {57assert(mprotect(buf, page_size, PROT_NONE) == 0);58return (buf + page_size);59}60}6162static void63freebuf(char *buf, size_t len, int guard_at_end)64{65size_t alloc_size, page_size;6667page_size = getpagesize();68alloc_size = roundup2(len, page_size) + page_size;6970if (guard_at_end)71munmap(buf + len + page_size - alloc_size, alloc_size);72else73munmap(buf - page_size, alloc_size);74}7576static void77test_stpncpy(const char *s, size_t size)78{79char *src, *dst, *expected;80size_t bufsize, x;81int i, j;8283for (i = 0; i <= 1; i++) {84for (j = 0; j <= 1; j++) {85for (bufsize = 0; bufsize <= size + 32; bufsize++) {86dst = makebuf(bufsize, j);87if (bufsize < size) {88src = makebuf(bufsize, i);89memcpy(src, s, bufsize);90expected = dst + bufsize;91} else {92src = makebuf(size, i);93memcpy(src, s, size);94expected = dst + size - 1;95}9697memset(dst, 'X', bufsize);98assert(stpncpy_fn(dst, src, bufsize) == expected);99assert(memcmp(src, dst, MIN(bufsize, size)) == 0);100for (x = size; x < bufsize; x++)101assert(dst[x] == '\0');102103freebuf(dst, bufsize, j);104freebuf(src, MIN(bufsize, size), i);105}106}107}108}109110static void111test_sentinel(char *dest, char *src, size_t destlen, size_t srclen)112{113size_t i;114const char *res, *wantres;115const char *fail = NULL;116117for (i = 0; i < srclen; i++)118/* src will never include (){} */119src[i] = '0' + i;120src[srclen] = '\0';121122/* source sentinels: not to be copied */123src[-1] = '(';124src[srclen+1] = ')';125126memset(dest, 0xee, destlen);127128/* destination sentinels: not to be touched */129dest[-1] = '{';130dest[destlen] = '}';131132wantres = dest + (srclen > destlen ? destlen : srclen);133res = stpncpy_fn(dest, src, destlen);134135if (dest[-1] != '{')136fail = "start sentinel overwritten";137else if (dest[destlen] != '}')138fail = "end sentinel overwritten";139else if (strncmp(src, dest, destlen) != 0)140fail = "string not copied correctly";141else if (res != wantres)142fail = "incorrect return value";143else for (i = srclen; i < destlen; i++)144if (dest[i] != '\0') {145fail = "incomplete NUL padding";146break;147}148149if (fail)150atf_tc_fail_nonfatal("%s\n"151"stpncpy(%p \"%s\", %p \"%s\", %zu) = %p (want %p)\n",152fail, dest, dest, src, src, destlen, res, wantres);153}154155ATF_TC_WITHOUT_HEAD(null);156ATF_TC_BODY(null, tc)157{158ATF_CHECK_EQ(stpncpy_fn(NULL, NULL, 0), NULL);159}160161ATF_TC_WITHOUT_HEAD(bounds);162ATF_TC_BODY(bounds, tc)163{164size_t i;165char buf[64];166167for (i = 0; i < sizeof(buf) - 1; i++) {168buf[i] = ' ' + i;169buf[i+1] = '\0';170test_stpncpy(buf, i + 2);171}172}173174ATF_TC_WITHOUT_HEAD(alignments);175ATF_TC_BODY(alignments, tc)176{177size_t srcalign, destalign, srclen, destlen;178char src[15+3+64]; /* 15 offsets + 64 max length + NUL + sentinels */179char dest[15+2+64]; /* 15 offsets + 64 max length + sentinels */180181for (srcalign = 0; srcalign < 16; srcalign++)182for (destalign = 0; destalign < 16; destalign++)183for (srclen = 0; srclen < 64; srclen++)184for (destlen = 0; destlen < 64; destlen++)185test_sentinel(dest+destalign+1,186src+srcalign+1, destlen, srclen);187}188189ATF_TP_ADD_TCS(tp)190{191void *dl_handle;192193dl_handle = dlopen(NULL, RTLD_LAZY);194stpncpy_fn = dlsym(dl_handle, "test_stpncpy");195if (stpncpy_fn == NULL)196stpncpy_fn = stpncpy;197198ATF_TP_ADD_TC(tp, null);199ATF_TP_ADD_TC(tp, bounds);200ATF_TP_ADD_TC(tp, alignments);201202return (atf_no_error());203}204205206