Merge branch 'dev' of /var/lib/git/libucore into dev

This commit is contained in:
Nils O. Selåsdal
2014-04-09 23:51:54 +02:00
5 changed files with 43 additions and 22 deletions
+8
View File
@@ -21,6 +21,14 @@ extern "C" {
char * char *
uc_read_file(const char *file_name, size_t *length, size_t max); 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 #ifdef __cplusplus
} }
#endif #endif
+2 -3
View File
@@ -6,9 +6,8 @@
//Gnerate a compiler error if the compile time //Gnerate a compiler error if the compile time
//constant expression fails //constant expression fails
#define UC_STATIC_ASSERT(expr) \ #define UC_STATIC_ASSERT(expr)\
enum { assert_static__ = 1/(expr) }; typedef int STATIC_ASSERT_FAILED[(expr) ? 1 : -1]
//MAX of a and b //MAX of a and b
#define UC_MAX(a,b) \ #define UC_MAX(a,b) \
+3 -3
View File
@@ -32,9 +32,9 @@ static void uc_ratelimit_refill(struct RateLimit *r, long current_ts)
//Find elapsed time //Find elapsed time
diff_period = current_ts - r->last_refill_ts; diff_period = current_ts - r->last_refill_ts;
if (diff_period > r->period + 1) { if (diff_period > r->period) {
//help prevent overflow when calculating available_tickets below //help prevent overflow when calculating available tickets below
diff_period = r->period + 1; diff_period = r->period;
} else if (diff_period < 0) { } else if (diff_period < 0) {
//time went backwards, skip this round //time went backwards, skip this round
diff_period = 0; diff_period = 0;
+22 -9
View File
@@ -1,4 +1,5 @@
#include <stdlib.h> #include <stdlib.h>
#include <limits.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
@@ -9,9 +10,8 @@
#define CHUNK_SZ 1024 #define CHUNK_SZ 1024
char * 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 *s = NULL;
char *p; char *p;
struct stat statb; struct stat statb;
@@ -19,25 +19,26 @@ uc_read_file(const char *f, size_t *length, size_t max)
*length = 0; *length = 0;
if ((fd = open(f, O_RDONLY)) == -1)
return NULL;
if (fstat(fd, &statb) == -1) { if (fstat(fd, &statb) == -1) {
goto out_close; goto out_close;
} }
if ((statb.st_mode & S_IFMT) == S_IFREG) { if ((statb.st_mode & S_IFMT) == S_IFREG) {
if ((size_t)statb.st_size > max) { if (statb.st_size > (off_t)max) {
errno = EMSGSIZE; errno = EMSGSIZE;
goto out_close; goto out_close;
} }
alloc_sz = (size_t) statb.st_size + 1; // + 1 to avoid one realloc if (statb.st_size > SSIZE_MAX) {
s = malloc((size_t) alloc_sz + 1); //+1 for nul terminator //otherwise we cannot detect the return value of read()
alloc_sz = SSIZE_MAX;
} else {
alloc_sz = statb.st_size;
}
} else { } else {
alloc_sz = CHUNK_SZ; 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) { if (s == NULL) {
goto out_close; goto out_close;
} }
@@ -80,3 +81,15 @@ out_close:
return s; 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);
}
+1
View File
@@ -13,6 +13,7 @@ START_TEST (test_read_file_non_existing_file)
size_t len; size_t len;
int saved_errno; int saved_errno;
errno = 0;
content = uc_read_file("non existing file 123765", &len, 1000000000); content = uc_read_file("non existing file 123765", &len, 1000000000);
saved_errno = errno; saved_errno = errno;