[Pcsclite-git-commit] [PCSC] 02/10: SYS_InitRandom(): new function to init the PRNG

Ludovic Rousseau rousseau at moszumanska.debian.org
Fri Aug 5 15:48:04 UTC 2016


This is an automated email from the git hooks/post-receive script.

rousseau pushed a commit to branch master
in repository PCSC.

commit 8b80aa4900cd60ab075802bdcc5a996027d0c74e
Author: Ludovic Rousseau <ludovic.rousseau at free.fr>
Date:   Thu Aug 4 11:23:37 2016 +0200

    SYS_InitRandom(): new function to init the PRNG
    
    The Pseudo Random Number Generator now has an independent function to
    initialize its internal state.
    
    The PRNG init was done in the first call to SYS_RandomInt().
    A problem could appear when SYS_RandomInt() is called for the first time
    at the same time from 2 clients.
    
    Thanks to Maksim Ivanov for the idea
    "[Pcsclite-muscle] Possible generation of duplicate SCARDHANDLE"
    http://lists.alioth.debian.org/pipermail/pcsclite-muscle/Week-of-Mon-20160801/000686.html
---
 src/sys_generic.h |  2 +-
 src/sys_unix.c    | 15 ++++++---------
 2 files changed, 7 insertions(+), 10 deletions(-)

diff --git a/src/sys_generic.h b/src/sys_generic.h
index f792c1d..c17c702 100644
--- a/src/sys_generic.h
+++ b/src/sys_generic.h
@@ -49,6 +49,6 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 	int SYS_RandomInt(int, int);
 
-	int SYS_GetSeed(void);
+	void SYS_InitRandom(void);
 
 #endif							/* __sys_generic_h__ */
diff --git a/src/sys_unix.c b/src/sys_unix.c
index 950f58d..b22dc55 100644
--- a/src/sys_unix.c
+++ b/src/sys_unix.c
@@ -98,15 +98,8 @@ INTERNAL int SYS_USleep(int iTimeVal)
 
 INTERNAL int SYS_RandomInt(int fStart, int fEnd)
 {
-	static int iInitialized = 0;
 	int iRandNum = 0;
 
-	if (0 == iInitialized)
-	{
-		srand(SYS_GetSeed());
-		iInitialized = 1;
-	}
-
 	if (-1 == fEnd)
 		/* full int range */
 		iRandNum = rand();
@@ -116,7 +109,10 @@ INTERNAL int SYS_RandomInt(int fStart, int fEnd)
 	return iRandNum;
 }
 
-INTERNAL int SYS_GetSeed(void)
+/**
+ * Initialize the random generator
+ */
+INTERNAL void SYS_InitRandom(void)
 {
 	struct timeval tv;
 	struct timezone tz;
@@ -131,6 +127,7 @@ INTERNAL int SYS_GetSeed(void)
 	{
 		myseed = (long) time(NULL);
 	}
-	return myseed;
+
+	srand(myseed);
 }
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pcsclite/PCSC.git



More information about the Pcsclite-cvs-commit mailing list