From owner-svn-src-stable@freebsd.org Sun May 29 06:46:18 2016 Return-Path: Delivered-To: svn-src-stable@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 4F532B536E8; Sun, 29 May 2016 06:46:18 +0000 (UTC) (envelope-from ache@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 06CC719BD; Sun, 29 May 2016 06:46:17 +0000 (UTC) (envelope-from ache@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id u4T6kHlZ013170; Sun, 29 May 2016 06:46:17 GMT (envelope-from ache@FreeBSD.org) Received: (from ache@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id u4T6kHaR013169; Sun, 29 May 2016 06:46:17 GMT (envelope-from ache@FreeBSD.org) Message-Id: <201605290646.u4T6kHaR013169@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: ache set sender to ache@FreeBSD.org using -f From: "Andrey A. Chernov" Date: Sun, 29 May 2016 06:46:17 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org Subject: svn commit: r300946 - stable/10/lib/libc/stdlib X-SVN-Group: stable-10 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable@freebsd.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: SVN commit messages for all the -stable branches of the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 29 May 2016 06:46:18 -0000 Author: ache Date: Sun May 29 06:46:17 2016 New Revision: 300946 URL: https://svnweb.freebsd.org/changeset/base/300946 Log: MFC: r300397 1) POSIX prohibits printing errors to stderr here and require returning NULL: "Upon successful completion, initstate() and setstate() shall return a pointer to the previous state array; otherwise, a null pointer shall be returned. Although some implementations of random() have written messages to standard error, such implementations do not conform to POSIX.1-2008." 2) Move error detections earlier to prevent state modifying. Modified: stable/10/lib/libc/stdlib/random.c Directory Properties: stable/10/ (props changed) Modified: stable/10/lib/libc/stdlib/random.c ============================================================================== --- stable/10/lib/libc/stdlib/random.c Sun May 29 06:29:22 2016 (r300945) +++ stable/10/lib/libc/stdlib/random.c Sun May 29 06:46:17 2016 (r300946) @@ -37,7 +37,6 @@ __FBSDID("$FreeBSD$"); #include #include #include -#include #include #include "un-namespace.h" @@ -341,15 +340,12 @@ initstate(unsigned long seed, char *arg_ char *ostate = (char *)(&state[-1]); uint32_t *int_arg_state = (uint32_t *)arg_state; + if (n < BREAK_0) + return (NULL); if (rand_type == TYPE_0) state[-1] = rand_type; else state[-1] = MAX_TYPES * (rptr - state) + rand_type; - if (n < BREAK_0) { - (void)fprintf(stderr, - "random: not enough state (%ld bytes); ignored.\n", n); - return (0); - } if (n < BREAK_1) { rand_type = TYPE_0; rand_deg = DEG_0; @@ -408,24 +404,23 @@ setstate(char *arg_state) uint32_t rear = new_state[0] / MAX_TYPES; char *ostate = (char *)(&state[-1]); - if (rand_type == TYPE_0) - state[-1] = rand_type; - else - state[-1] = MAX_TYPES * (rptr - state) + rand_type; switch(type) { case TYPE_0: case TYPE_1: case TYPE_2: case TYPE_3: case TYPE_4: - rand_type = type; - rand_deg = degrees[type]; - rand_sep = seps[type]; break; default: - (void)fprintf(stderr, - "random: state info corrupted; not changed.\n"); + return (NULL); } + if (rand_type == TYPE_0) + state[-1] = rand_type; + else + state[-1] = MAX_TYPES * (rptr - state) + rand_type; + rand_type = type; + rand_deg = degrees[type]; + rand_sep = seps[type]; state = new_state + 1; if (rand_type != TYPE_0) { rptr = &state[rear];