Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 19 Aug 2017 09:38:43 +0000 (UTC)
From:      Konstantin Belousov <kib@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-11@freebsd.org
Subject:   svn commit: r322692 - in stable/11/lib/libc: stdlib string tests/string
Message-ID:  <201708190938.v7J9chGd037675@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: kib
Date: Sat Aug 19 09:38:43 2017
New Revision: 322692
URL: https://svnweb.freebsd.org/changeset/base/322692

Log:
  MFC r322427:
  Improve standard compliance for memset_s() and abort_handler_s().

Modified:
  stable/11/lib/libc/stdlib/set_constraint_handler_s.c
  stable/11/lib/libc/string/memset_s.c
  stable/11/lib/libc/tests/string/memset_s_test.c
Directory Properties:
  stable/11/   (props changed)

Modified: stable/11/lib/libc/stdlib/set_constraint_handler_s.c
==============================================================================
--- stable/11/lib/libc/stdlib/set_constraint_handler_s.c	Sat Aug 19 01:49:09 2017	(r322691)
+++ stable/11/lib/libc/stdlib/set_constraint_handler_s.c	Sat Aug 19 09:38:43 2017	(r322692)
@@ -33,6 +33,8 @@ __FBSDID("$FreeBSD$");
 #include <pthread.h>
 #include <stddef.h>
 #include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
 #include "un-namespace.h"
 #include "libc_private.h"
 
@@ -81,10 +83,14 @@ __throw_constraint_handler_s(const char * restrict msg
 }
 
 void
-abort_handler_s(const char * restrict msg __unused,
-    void * restrict ptr __unused, errno_t error __unused)
+abort_handler_s(const char * restrict msg, void * restrict ptr __unused,
+    errno_t error __unused)
 {
+	static const char ahs[] = "abort_handler_s : ";
 
+	(void) _write(STDERR_FILENO, ahs, sizeof(ahs) - 1);
+	(void) _write(STDERR_FILENO, msg, strlen(msg));
+	(void) _write(STDERR_FILENO, "\n", 1);
 	abort();
 }
 

Modified: stable/11/lib/libc/string/memset_s.c
==============================================================================
--- stable/11/lib/libc/string/memset_s.c	Sat Aug 19 01:49:09 2017	(r322691)
+++ stable/11/lib/libc/string/memset_s.c	Sat Aug 19 09:38:43 2017	(r322692)
@@ -42,7 +42,7 @@ memset_s(void *s, rsize_t smax, int c, rsize_t n)
 	volatile unsigned char *dst;
 
 	ret = EINVAL;
-	lim = smax;
+	lim = n < smax ? n : smax;
 	v = (unsigned char)c;
 	dst = (unsigned char *)s;
 	if (s == NULL) {
@@ -53,11 +53,14 @@ memset_s(void *s, rsize_t smax, int c, rsize_t n)
 	} else if (n > RSIZE_MAX) {
 		__throw_constraint_handler_s("memset_s : n > RSIZE_MAX", ret);
 	} else {
-		if (n < smax)
-			lim = n;
 		while (lim > 0)
 			dst[--lim] = v;
-		ret = 0;
+		if (n > smax) {
+			__throw_constraint_handler_s("memset_s : n > smax",
+			    ret);
+		} else {
+			ret = 0;
+		}
 	}
 	return (ret);
 }

Modified: stable/11/lib/libc/tests/string/memset_s_test.c
==============================================================================
--- stable/11/lib/libc/tests/string/memset_s_test.c	Sat Aug 19 01:49:09 2017	(r322691)
+++ stable/11/lib/libc/tests/string/memset_s_test.c	Sat Aug 19 09:38:43 2017	(r322692)
@@ -109,13 +109,18 @@ ATF_TC_BODY(n_lt_smax, tc)
 	assert(b[2] == 3);
 }
 
-/* n > smax */
+/* n > smax, handler */
 ATF_TC_WITHOUT_HEAD(n_gt_smax);
 ATF_TC_BODY(n_gt_smax, tc)
 {
 	char b[3] = {1, 2, 3};
 
-	assert(memset_s(&b[0], 1, 9, 3) == 0);
+	e = 0;
+	m = NULL;
+	set_constraint_handler_s(h);
+	assert(memset_s(&b[0], 1, 9, 3) != 0);
+	assert(e > 0);
+	assert(strcmp(m, "memset_s : n > smax") == 0);
 	assert(b[0] == 9);
 	assert(b[1] == 2);
 	assert(b[2] == 3);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201708190938.v7J9chGd037675>