From b8f12a6856f9fb4d2b36a8174f5f80fc724c871f Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Sat, 21 Nov 2015 01:08:36 +0100 Subject: [PATCH] Fix fread/fwrite zero division when size is zero. --- libc/stdio/fread_unlocked.cpp | 2 ++ libc/stdio/fwrite_unlocked.cpp | 2 ++ 2 files changed, 4 insertions(+) diff --git a/libc/stdio/fread_unlocked.cpp b/libc/stdio/fread_unlocked.cpp index c9d6e5e5..a070ca66 100644 --- a/libc/stdio/fread_unlocked.cpp +++ b/libc/stdio/fread_unlocked.cpp @@ -40,6 +40,8 @@ size_t fread_unlocked(void* ptr, unsigned char* buf = (unsigned char*) ptr; size_t count = element_size * num_elements; + if ( count == 0 ) + return num_elements; if ( fp->buffer_mode == _IONBF ) { diff --git a/libc/stdio/fwrite_unlocked.cpp b/libc/stdio/fwrite_unlocked.cpp index 2dad4f68..2e25222a 100644 --- a/libc/stdio/fwrite_unlocked.cpp +++ b/libc/stdio/fwrite_unlocked.cpp @@ -40,6 +40,8 @@ size_t fwrite_unlocked(const void* ptr, const unsigned char* buf = (const unsigned char*) ptr; size_t count = element_size * num_elements; + if ( count == 0 ) + return num_elements; if ( fp->buffer_mode == _IONBF ) {