From 35654c7b2138b590cdc18898a9897b665d8a443e Mon Sep 17 00:00:00 2001 From: Brian Watling Date: Wed, 5 Nov 2014 15:17:10 -0800 Subject: [PATCH] Remove memory leak from readRandomDevice by using a raw fd Summary: Switch from dynamically allocating a File to using a raw fd for readRandomDevice. This prevents races at shutdown while also eliminating the memory leak Test Plan: run unit tests Reviewed By: meyering@fb.com Subscribers: njormrod, folly-diffs@, tao-eng@ FB internal diff: D1662151 Signature: t1:1662151:1415229242:525b6294b27bb68b5dda70aadb8d3ba1cc61b815 --- folly/Random.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/folly/Random.cpp b/folly/Random.cpp index 4ed578c3..fcebfde9 100644 --- a/folly/Random.cpp +++ b/folly/Random.cpp @@ -31,10 +31,10 @@ namespace folly { namespace { void readRandomDevice(void* data, size_t size) { - // Keep it open for the duration of the program. Note that we leak the File - // to ensure the file is indeed open for the duration of the program. - static File& randomDevice = *new File("/dev/urandom"); - auto bytesRead = readFull(randomDevice.fd(), data, size); + // Keep the random device open for the duration of the program. + static int randomFd = ::open("/dev/urandom", O_RDONLY); + PCHECK(randomFd >= 0); + auto bytesRead = readFull(randomFd, data, size); PCHECK(bytesRead >= 0 && size_t(bytesRead) == size); } -- 2.34.1