Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 22 May 2016 06:18:59 +0000 (UTC)
From:      "Andrey A. Chernov" <ache@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r300397 - head/lib/libc/stdlib
Message-ID:  <201605220618.u4M6Ixif056869@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: ache
Date: Sun May 22 06:18:59 2016
New Revision: 300397
URL: https://svnweb.freebsd.org/changeset/base/300397

Log:
  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.
  
  MFC after:      1 week

Modified:
  head/lib/libc/stdlib/random.c

Modified: head/lib/libc/stdlib/random.c
==============================================================================
--- head/lib/libc/stdlib/random.c	Sun May 22 04:28:55 2016	(r300396)
+++ head/lib/libc/stdlib/random.c	Sun May 22 06:18:59 2016	(r300397)
@@ -37,7 +37,6 @@ __FBSDID("$FreeBSD$");
 #include <sys/param.h>
 #include <sys/sysctl.h>
 #include <stdint.h>
-#include <stdio.h>
 #include <stdlib.h>
 #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];



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