Skip to content

Commit

Permalink
Improve assertion messages from Random::secureRandom
Browse files Browse the repository at this point in the history
Summary:
When the PCHECK for read() in Random::secureRandom fails, the message is often misleading.

In the following case, bytesRead != size caused the assertion failure. Unfortunately, the message includes an unrelated error message ("No such file or directory"), which can throw people off:

  F1031 13:15:26.997059 270573 Random.cpp:69] Check failed: bytesRead >= 0 && size_t(bytesRead) == size : No such file or directory [2]

Improve the message by splitting the PCHECK into a PCHECK (for the read() error) and a CHECK_EQ (for the logic error):

  F1108 17:33:31.771008 3185333 Random.cpp:70] Check failed: size_t(bytesRead) == size (112960 vs. 1048576)

Reviewed By: yfeldblum, simpkins

Differential Revision: D12856276

fbshipit-source-id: fe9067972db805a54f2cc290aab11bac425601ef
  • Loading branch information
Matt Glazar authored and facebook-github-bot committed Nov 12, 2018
1 parent 613f048 commit 486c659
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion folly/Random.cpp
Expand Up @@ -66,7 +66,8 @@ void readRandomDevice(void* data, size_t size) {
static int randomFd = ::open("/dev/urandom", O_RDONLY | O_CLOEXEC);
PCHECK(randomFd >= 0);
auto bytesRead = readFull(randomFd, data, size);
PCHECK(bytesRead >= 0 && size_t(bytesRead) == size);
PCHECK(bytesRead >= 0);
CHECK_EQ(size_t(bytesRead), size);
#endif
}

Expand Down

0 comments on commit 486c659

Please sign in to comment.