Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src Add some fundamental checks for memset(3). Prompted by a rec...
details: https://anonhg.NetBSD.org/src/rev/7e67f2f25533
branches: trunk
changeset: 765700:7e67f2f25533
user: jruoho <jruoho%NetBSD.org@localhost>
date: Fri Jun 03 06:39:52 2011 +0000
description:
Add some fundamental checks for memset(3). Prompted by a recent nasty
bug in the Google Android libc.
diffstat:
distrib/sets/lists/tests/mi | 4 +-
tests/lib/libc/string/Makefile | 3 +-
tests/lib/libc/string/t_memset.c | 192 +++++++++++++++++++++++++++++++++++++++
3 files changed, 197 insertions(+), 2 deletions(-)
diffs (238 lines):
diff -r 8c31e52e8796 -r 7e67f2f25533 distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi Fri Jun 03 05:50:07 2011 +0000
+++ b/distrib/sets/lists/tests/mi Fri Jun 03 06:39:52 2011 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.345 2011/06/03 05:42:09 jruoho Exp $
+# $NetBSD: mi,v 1.346 2011/06/03 06:39:52 jruoho Exp $
#
# Note: don't delete entries from here - mark them as "obsolete" instead.
#
@@ -446,6 +446,7 @@
./usr/libdata/debug/usr/tests/lib/libc/string tests-lib-debug
./usr/libdata/debug/usr/tests/lib/libc/string/t_memcpy.debug tests-lib-debug debug,atf
./usr/libdata/debug/usr/tests/lib/libc/string/t_memmem.debug tests-lib-debug debug,atf
+./usr/libdata/debug/usr/tests/lib/libc/string/t_memset.debug tests-lib-debug debug,atf
./usr/libdata/debug/usr/tests/lib/libc/string/t_popcount.debug tests-lib-debug debug,atf
./usr/libdata/debug/usr/tests/lib/libc/string/t_strerror.debug tests-lib-debug debug,atf
./usr/libdata/debug/usr/tests/lib/libc/string/t_stresep.debug tests-lib-debug debug,atf
@@ -1960,6 +1961,7 @@
./usr/tests/lib/libc/string/Atffile tests-lib-tests atf
./usr/tests/lib/libc/string/t_memcpy tests-lib-tests atf
./usr/tests/lib/libc/string/t_memmem tests-lib-tests atf
+./usr/tests/lib/libc/string/t_memset tests-lib-tests atf
./usr/tests/lib/libc/string/t_popcount tests-lib-tests atf
./usr/tests/lib/libc/string/t_strerror tests-lib-tests atf
./usr/tests/lib/libc/string/t_stresep tests-lib-tests atf
diff -r 8c31e52e8796 -r 7e67f2f25533 tests/lib/libc/string/Makefile
--- a/tests/lib/libc/string/Makefile Fri Jun 03 05:50:07 2011 +0000
+++ b/tests/lib/libc/string/Makefile Fri Jun 03 06:39:52 2011 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.5 2011/05/09 06:04:14 jruoho Exp $
+# $NetBSD: Makefile,v 1.6 2011/06/03 06:39:52 jruoho Exp $
.include <bsd.own.mk>
@@ -6,6 +6,7 @@
TESTS_C+= t_memcpy
TESTS_C+= t_memmem
+TESTS_C+= t_memset
TESTS_C+= t_popcount
TESTS_C+= t_strerror
TESTS_C+= t_stresep
diff -r 8c31e52e8796 -r 7e67f2f25533 tests/lib/libc/string/t_memset.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/libc/string/t_memset.c Fri Jun 03 06:39:52 2011 +0000
@@ -0,0 +1,192 @@
+/* $NetBSD: t_memset.c,v 1.1 2011/06/03 06:39:52 jruoho Exp $ */
+
+/*-
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jukka Ruohonen.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: t_memset.c,v 1.1 2011/06/03 06:39:52 jruoho Exp $");
+
+#include <sys/stat.h>
+
+#include <atf-c.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+static long page = 0;
+static void fill(char *, size_t, char);
+static bool check(char *, size_t, char);
+
+ATF_TC(memset_array);
+ATF_TC_HEAD(memset_array, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Test memset(3) with arrays");
+}
+
+ATF_TC_BODY(memset_array, tc)
+{
+ char buf[1024];
+
+ (void)memset(buf, 0, sizeof(buf));
+
+ if (check(buf, sizeof(buf), 0) != true)
+ atf_tc_fail("memset(3) did not fill a static buffer");
+
+ (void)memset(buf, 'x', sizeof(buf));
+
+ if (check(buf, sizeof(buf), 'x') != true)
+ atf_tc_fail("memset(3) did not fill a static buffer");
+}
+
+ATF_TC(memset_basic);
+ATF_TC_HEAD(memset_basic, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "A basic test of memset(3)");
+}
+
+ATF_TC_BODY(memset_basic, tc)
+{
+ char *buf, *ret;
+
+ buf = malloc(page);
+ ret = malloc(page);
+
+ ATF_REQUIRE(buf != NULL);
+ ATF_REQUIRE(ret != NULL);
+
+ fill(ret, page, 0);
+ memset(buf, 0, page);
+
+ ATF_REQUIRE(memcmp(ret, buf, page) == 0);
+
+ fill(ret, page, 'x');
+ memset(buf, 'x', page);
+
+ ATF_REQUIRE(memcmp(ret, buf, page) == 0);
+
+ free(buf);
+ free(ret);
+}
+
+ATF_TC(memset_nonzero);
+ATF_TC_HEAD(memset_nonzero, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Test memset(3) with non-zero params");
+}
+
+ATF_TC_BODY(memset_nonzero, tc)
+{
+ const size_t n = 0x7f;
+ char *buf;
+ size_t i;
+
+ buf = malloc(page);
+ ATF_REQUIRE(buf != NULL);
+
+ for (i = 0x21; i < n; i++) {
+
+ (void)memset(buf, i, page);
+
+ if (check(buf, page, i) != true)
+ atf_tc_fail("memset(3) did not fill properly");
+ }
+
+ free(buf);
+}
+
+ATF_TC(memset_struct);
+ATF_TC_HEAD(memset_struct, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Test memset(3) with a structure");
+}
+
+ATF_TC_BODY(memset_struct, tc)
+{
+ struct stat st;
+
+ st.st_dev = 0;
+ st.st_ino = 1;
+ st.st_mode = 2;
+ st.st_nlink = 3;
+ st.st_uid = 4;
+ st.st_gid = 5;
+ st.st_rdev = 6;
+ st.st_size = 7;
+ st.st_atime = 8;
+ st.st_mtime = 9;
+
+ (void)memset(&st, 0, sizeof(struct stat));
+
+ ATF_REQUIRE(st.st_dev == 0);
+ ATF_REQUIRE(st.st_ino == 0);
+ ATF_REQUIRE(st.st_mode == 0);
+ ATF_REQUIRE(st.st_nlink == 0);
+ ATF_REQUIRE(st.st_uid == 0);
+ ATF_REQUIRE(st.st_gid == 0);
+ ATF_REQUIRE(st.st_rdev == 0);
+ ATF_REQUIRE(st.st_size == 0);
+ ATF_REQUIRE(st.st_atime == 0);
+ ATF_REQUIRE(st.st_mtime == 0);
+}
+
+static void
+fill(char *buf, size_t len, char x)
+{
+ size_t i;
+
+ for (i = 0; i < len; i++)
+ buf[i] = x;
+}
+
+static bool
+check(char *buf, size_t len, char x)
+{
+ size_t i;
+
+ for (i = 0; i < len; i++) {
+
+ if (buf[i] != x)
+ return false;
+ }
+
+ return true;
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+ page = sysconf(_SC_PAGESIZE);
+ ATF_REQUIRE(page >= 0);
+
+ ATF_TP_ADD_TC(tp, memset_array);
+ ATF_TP_ADD_TC(tp, memset_basic);
+ ATF_TP_ADD_TC(tp, memset_nonzero);
+ ATF_TP_ADD_TC(tp, memset_struct);
+
+ return atf_no_error();
+}
Home |
Main Index |
Thread Index |
Old Index