From 62a7a6071294ef63604a632e3cdebcf84b7456df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Thu, 13 Mar 2014 23:04:14 +0100 Subject: [PATCH 1/5] Set errno to 0, for consistent results --- test/test_read_file.c | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test_read_file.c b/test/test_read_file.c index 5bef1f4..dcd8d68 100644 --- a/test/test_read_file.c +++ b/test/test_read_file.c @@ -13,6 +13,7 @@ START_TEST (test_read_file_non_existing_file) size_t len; int saved_errno; + errno = 0; content = uc_read_file("non existing file 123765", &len, 1000000000); saved_errno = errno; From 092074bbfca05d2063cb15e39f6bf4abcbdb8939 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Thu, 13 Mar 2014 23:07:57 +0100 Subject: [PATCH 2/5] Fix read_file in case file size if > SSIZE_MAX --- src/read_file.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/read_file.c b/src/read_file.c index cf6c3f5..62f0d81 100644 --- a/src/read_file.c +++ b/src/read_file.c @@ -1,10 +1,11 @@ -#include -#include -#include -#include -#include -#include -#include "ucore/read_file.h" +#include +#include +#include +#include +#include +#include +#include +#include "ucore/read_file.h" #define CHUNK_SZ 1024 @@ -27,17 +28,21 @@ uc_read_file(const char *f, size_t *length, size_t max) } if ((statb.st_mode & S_IFMT) == S_IFREG) { - if ((size_t)statb.st_size > max) { + if (statb.st_size > (off_t)max) { errno = EMSGSIZE; goto out_close; } - alloc_sz = (size_t) statb.st_size + 1; // + 1 to avoid one realloc - s = malloc((size_t) alloc_sz + 1); //+1 for nul terminator + if (statb.st_size > SSIZE_MAX) { + //otherwise we cannot detect the return value of read() + alloc_sz = SSIZE_MAX; + } else { + alloc_sz = statb.st_size; + } } else { alloc_sz = CHUNK_SZ; - s = malloc(alloc_sz + 1); //+1 for nul terminator } + s = malloc(alloc_sz + 1); //+1 for nul terminator if (s == NULL) { goto out_close; } From b93e5b6d5b2a8d598eaca29c20b697498e75a906 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Thu, 13 Mar 2014 23:16:50 +0100 Subject: [PATCH 3/5] add uc_read_fd function --- include/ucore/read_file.h | 8 ++++++++ src/read_file.c | 18 +++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/include/ucore/read_file.h b/include/ucore/read_file.h index 9e1ea10..26e4987 100644 --- a/include/ucore/read_file.h +++ b/include/ucore/read_file.h @@ -21,6 +21,14 @@ extern "C" { char * uc_read_file(const char *file_name, size_t *length, size_t max); +/** Read the content of a file descriptor as a stream. + * @see uc_read_file + * This is similar but reads from a file descriptor, + * of any type as long as it supports the read() call. + */ +char * +uc_read_fd(int fd, size_t *length, size_t max); + #ifdef __cplusplus } #endif diff --git a/src/read_file.c b/src/read_file.c index 62f0d81..2bdb9e5 100644 --- a/src/read_file.c +++ b/src/read_file.c @@ -10,9 +10,8 @@ #define CHUNK_SZ 1024 char * -uc_read_file(const char *f, size_t *length, size_t max) +uc_read_fd(int fd, size_t *length, size_t max) { - int fd; char *s = NULL; char *p; struct stat statb; @@ -20,9 +19,6 @@ uc_read_file(const char *f, size_t *length, size_t max) *length = 0; - if ((fd = open(f, O_RDONLY)) == -1) - return NULL; - if (fstat(fd, &statb) == -1) { goto out_close; } @@ -85,3 +81,15 @@ out_close: return s; } +char * +uc_read_file(const char *f, size_t *length, size_t max) +{ + int fd; + + if ((fd = open(f, O_RDONLY)) == -1) { + *length = 0; + return NULL; + } + + return uc_read_fd(fd, length, max); +} From 71c9643ee9c68f0fb94dced4b11770ada55fda9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Fri, 14 Mar 2014 00:58:09 +0100 Subject: [PATCH 4/5] Better error message for UC_STATIC_ASSERT. Also require it to be terminated by a ; --- include/ucore/utils.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/include/ucore/utils.h b/include/ucore/utils.h index e2b6592..70e5ca9 100644 --- a/include/ucore/utils.h +++ b/include/ucore/utils.h @@ -6,9 +6,8 @@ //Gnerate a compiler error if the compile time //constant expression fails -#define UC_STATIC_ASSERT(expr) \ - enum { assert_static__ = 1/(expr) }; - +#define UC_STATIC_ASSERT(expr)\ + typedef int STATIC_ASSERT_FAILED[(expr) ? 1 : -1] //MAX of a and b #define UC_MAX(a,b) \ From c9656fbb3c87fc5cc779bcf53311879d76c384e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Tue, 18 Mar 2014 22:33:32 +0100 Subject: [PATCH 5/5] no need for clamping to funds + 1, just to funds --- src/rate_limit.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/rate_limit.c b/src/rate_limit.c index 2facdc1..a2ad6ef 100644 --- a/src/rate_limit.c +++ b/src/rate_limit.c @@ -32,9 +32,9 @@ static void uc_ratelimit_refill(struct RateLimit *r, long current_ts) //Find elapsed time diff_period = current_ts - r->last_refill_ts; - if (diff_period > r->period + 1) { - //help prevent overflow when calculating available_tickets below - diff_period = r->period + 1; + if (diff_period > r->period) { + //help prevent overflow when calculating available tickets below + diff_period = r->period; } else if (diff_period < 0) { //time went backwards, skip this round diff_period = 0;