From owner-svn-src-all@freebsd.org  Sat Aug 12 15:18:18 2017
Return-Path: <owner-svn-src-all@freebsd.org>
Delivered-To: svn-src-all@mailman.ysv.freebsd.org
Received: from mx1.freebsd.org (mx1.freebsd.org
 [IPv6:2001:1900:2254:206a::19:1])
 by mailman.ysv.freebsd.org (Postfix) with ESMTP id 78C82DDCAF4;
 Sat, 12 Aug 2017 15:18:18 +0000 (UTC) (envelope-from kib@FreeBSD.org)
Received: from repo.freebsd.org (repo.freebsd.org
 [IPv6:2610:1c1:1:6068::e6a:0])
 (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
 (Client did not present a certificate)
 by mx1.freebsd.org (Postfix) with ESMTPS id 54A0B64755;
 Sat, 12 Aug 2017 15:18:18 +0000 (UTC) (envelope-from kib@FreeBSD.org)
Received: from repo.freebsd.org ([127.0.1.37])
 by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id v7CFIHiP035167;
 Sat, 12 Aug 2017 15:18:17 GMT (envelope-from kib@FreeBSD.org)
Received: (from kib@localhost)
 by repo.freebsd.org (8.15.2/8.15.2/Submit) id v7CFIHld035164;
 Sat, 12 Aug 2017 15:18:17 GMT (envelope-from kib@FreeBSD.org)
Message-Id: <201708121518.v7CFIHld035164@repo.freebsd.org>
X-Authentication-Warning: repo.freebsd.org: kib set sender to kib@FreeBSD.org
 using -f
From: Konstantin Belousov <kib@FreeBSD.org>
Date: Sat, 12 Aug 2017 15:18:17 +0000 (UTC)
To: src-committers@freebsd.org, svn-src-all@freebsd.org,
 svn-src-head@freebsd.org
Subject: svn commit: r322427 - in head/lib/libc: stdlib string tests/string
X-SVN-Group: head
X-SVN-Commit-Author: kib
X-SVN-Commit-Paths: in head/lib/libc: stdlib string tests/string
X-SVN-Commit-Revision: 322427
X-SVN-Commit-Repository: base
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-BeenThere: svn-src-all@freebsd.org
X-Mailman-Version: 2.1.23
Precedence: list
List-Id: "SVN commit messages for the entire src tree \(except for &quot;
 user&quot; and &quot; projects&quot; \)" <svn-src-all.freebsd.org>
List-Unsubscribe: <https://lists.freebsd.org/mailman/options/svn-src-all>,
 <mailto:svn-src-all-request@freebsd.org?subject=unsubscribe>
List-Archive: <http://lists.freebsd.org/pipermail/svn-src-all/>
List-Post: <mailto:svn-src-all@freebsd.org>
List-Help: <mailto:svn-src-all-request@freebsd.org?subject=help>
List-Subscribe: <https://lists.freebsd.org/mailman/listinfo/svn-src-all>,
 <mailto:svn-src-all-request@freebsd.org?subject=subscribe>
X-List-Received-Date: Sat, 12 Aug 2017 15:18:18 -0000

Author: kib
Date: Sat Aug 12 15:18:17 2017
New Revision: 322427
URL: https://svnweb.freebsd.org/changeset/base/322427

Log:
  Improve standard compliance for memset_s() and abort_handler_s().
  
  abort_handler_s() currently simply calls abort(), though the standard
  specifies more: "Writes an implementation-defined message to stderr
  which must include the string pointed to by msg and calls abort()."
  
  memset_s() is missing error condition "n > smax", and does not invoke
  the constraint handler after filling the buffer: "following errors are
  detected at runtime and call the currently installed constraint
  handler function after storing ch in every location of the destination
  range [dest, dest+destsz) if dest and destsz are themselves valid",
  one of the errors is "n > smax" itself.
  
  Submitted by:	Yuri Pankov <yuripv@gmx.com>
  MFC after:	1 week
  Differential revision:	https://reviews.freebsd.org/D11991

Modified:
  head/lib/libc/stdlib/set_constraint_handler_s.c
  head/lib/libc/string/memset_s.c
  head/lib/libc/tests/string/memset_s_test.c

Modified: head/lib/libc/stdlib/set_constraint_handler_s.c
==============================================================================
--- head/lib/libc/stdlib/set_constraint_handler_s.c	Sat Aug 12 14:58:09 2017	(r322426)
+++ head/lib/libc/stdlib/set_constraint_handler_s.c	Sat Aug 12 15:18:17 2017	(r322427)
@@ -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: head/lib/libc/string/memset_s.c
==============================================================================
--- head/lib/libc/string/memset_s.c	Sat Aug 12 14:58:09 2017	(r322426)
+++ head/lib/libc/string/memset_s.c	Sat Aug 12 15:18:17 2017	(r322427)
@@ -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: head/lib/libc/tests/string/memset_s_test.c
==============================================================================
--- head/lib/libc/tests/string/memset_s_test.c	Sat Aug 12 14:58:09 2017	(r322426)
+++ head/lib/libc/tests/string/memset_s_test.c	Sat Aug 12 15:18:17 2017	(r322427)
@@ -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);