From 5417e40163e81a2fa424dbaeca009e2caadc333b Mon Sep 17 00:00:00 2001 From: BearLin Date: Mon, 7 Mar 2016 23:21:31 +0800 Subject: [PATCH 1/3] Add some protections in _xdb_read_data --- libscws/xdb.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/libscws/xdb.c b/libscws/xdb.c index 71273bd..5e7001e 100644 --- a/libscws/xdb.c +++ b/libscws/xdb.c @@ -57,8 +57,10 @@ static void _xdb_read_data(xdb_t x, void *buf, unsigned int off, int len) if (x->fd >= 0) { - lseek(x->fd, off, SEEK_SET); - read(x->fd, buf, len); + if (lseek(x->fd, off, SEEK_SET) == -1) + return; + if (read(x->fd, buf, len) != len) + return; } else { From 43b977d1269400e67ca854ae93943c40e61d45b9 Mon Sep 17 00:00:00 2001 From: BearLin Date: Mon, 7 Mar 2016 23:38:33 +0800 Subject: [PATCH 2/3] xdb_open add O_BINARY flag for cygwin/windows --- libscws/xdb.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/libscws/xdb.c b/libscws/xdb.c index 5e7001e..0aaf091 100644 --- a/libscws/xdb.c +++ b/libscws/xdb.c @@ -6,11 +6,11 @@ */ #ifdef HAVE_CONFIG_H -# include "config.h" +#include "config.h" #endif #ifdef WIN32 -# include "config_win32.h" +#include "config_win32.h" #endif #include "xdb.h" @@ -18,7 +18,8 @@ #include #include #ifndef WIN32 -# include +#include +#include #endif #include #include @@ -26,11 +27,11 @@ #include #ifdef HAVE_FLOCK -# include +#include #endif #ifdef HAVE_MMAP -# include +#include #endif static int _xdb_hasher(xdb_t x, const char *s, int len) @@ -134,7 +135,11 @@ xdb_t xdb_open(const char *fpath, int mode) return NULL; /* try to open & check the file */ +#ifdef WIN32 + if ((x->fd = open(fpath, mode == 'w' ? O_RDWR|O_BINARY : O_RDONLY|O_BINARY)) < 0) +#else //WIN32 if ((x->fd = open(fpath, mode == 'w' ? O_RDWR : O_RDONLY)) < 0) +#endif //WIN32 { #ifdef DEBUG perror("Failed to open the XDB file"); From 1535e1e370540803942fa7c47f41a62561d9faf4 Mon Sep 17 00:00:00 2001 From: BearLin Date: Tue, 8 Mar 2016 17:58:55 +0800 Subject: [PATCH 3/3] Unify macro O_BINARY for xdb_open --- libscws/xdb.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/libscws/xdb.c b/libscws/xdb.c index 0aaf091..44dd207 100644 --- a/libscws/xdb.c +++ b/libscws/xdb.c @@ -34,6 +34,10 @@ #include #endif +#ifndef O_BINARY +#define O_BINARY 0 +#endif + static int _xdb_hasher(xdb_t x, const char *s, int len) { unsigned int h = x->base; @@ -135,11 +139,7 @@ xdb_t xdb_open(const char *fpath, int mode) return NULL; /* try to open & check the file */ -#ifdef WIN32 - if ((x->fd = open(fpath, mode == 'w' ? O_RDWR|O_BINARY : O_RDONLY|O_BINARY)) < 0) -#else //WIN32 - if ((x->fd = open(fpath, mode == 'w' ? O_RDWR : O_RDONLY)) < 0) -#endif //WIN32 + if ((x->fd = open(fpath, mode == 'w' ? O_RDWR | O_BINARY : O_RDONLY | O_BINARY)) < 0) { #ifdef DEBUG perror("Failed to open the XDB file");