diff options
Diffstat (limited to 'src/slib.c')
-rw-r--r-- | src/slib.c | 102 |
1 files changed, 94 insertions, 8 deletions
@@ -1,7 +1,16 @@ #include "config_xor.h" #if defined(HAVE_POSIX_FADVISE) && defined(HAVE_MINCORE) + +#if defined(__sun) || defined(__sun__) || defined(sun) +#define _XOPEN_SOURCE 500 +#else #define _XOPEN_SOURCE 600 +#endif + +#if defined(__GNUC__) +#define _DEFAULT_SOURCE +#endif #define _BSD_SOURCE #endif @@ -247,9 +256,9 @@ int dlog (int flag, const char * file, int line, const char *fmt, ...) if (flag == 1) { sl_snprintf (val, 81, _("\n--------- %10s "), file); - sl_strlcpy (msg, val, 80); + sl_strlcpy (msg, val, sizeof(msg)); sl_snprintf (val, 81, _(" --- %6d ---------\n"), line); - sl_strlcat (msg, val, 80); + sl_strlcat (msg, val, sizeof(msg)); sh_log_console (msg); } @@ -578,11 +587,37 @@ int sl_get_cap_qdel() { return 0; } /* * Have memset in a different translation unit (i.e. this) to prevent - * it to get optimized away + * it to get optimized away ...not safe with link-time optimisation... */ -void *sl_memset(void *s, int c, size_t n) +void * sl_memset(void *s, int c, size_t n) { - return memset(s, c,n); + /* See: + * https://www.usenix.org/sites/default/files/conference/protected-files/usenixsecurity17_slides_zhaomo_yang.pdf + */ +#if defined(HAVE_EXPLICIT_MEMSET) + return explicit_memset(s, c, n); +#elif defined(HAVE_EXPLICIT_BZERO) + if (c == 0) { + explicit_bzero(s, n); + return s; + } else { + return memset(s, c, n); + } +#elif defined(__GNUC__) + memset(s, c, n); + __asm__ __volatile__ ("" ::"r"(s): "memory"); /* compiler barrier */ + return s; +#else + if (c == 0) { + size_t i; + volatile unsigned char * t_s = (volatile unsigned char *)s; + for (i=0; i<n; ++i) + t_s[i] = 0; + return s; + } else { + return memset(s, c, n); + } +#endif } @@ -1069,6 +1104,40 @@ int sl_strcmp(const char * a, const char * b) return (-7); /* default to not equal */ } +/* Does not report sign. */ +int sl_ts_strncmp(const char * a, const char * b, size_t n) +{ +#ifdef SL_FAIL_ON_ERROR + SL_REQUIRE (a != NULL, _("a != NULL")); + SL_REQUIRE (b != NULL, _("b != NULL")); + SL_REQUIRE (n > 0, _("n > 0")); +#endif + + if (a != NULL && b != NULL) + { + const unsigned char *a1 = (const unsigned char *)a; + const unsigned char *b1 = (const unsigned char *)b; + size_t i; + int retval=0; + /* The simple index based access is optimized best by the + * compiler (tested with gcc 7.3.0). */ + for (i = 0; i < n; ++i) + { + if (a1[i] == '\0' || b1[i] == '\0') + break; + retval |= (a1[i] ^ b1[i]); + } + /* if (retval == 0) --> false (0) */ + return (retval != 0); + } + else if (a == NULL && b != NULL) + return (-1); + else if (a != NULL && b == NULL) + return (1); + else + return (-7); /* default to not equal */ +} + int sl_strncmp(const char * a, const char * b, size_t n) { #ifdef SL_FAIL_ON_ERROR @@ -1767,6 +1836,7 @@ SL_TICKET sl_make_ticket (const char * ofile, int oline, { free (ofiles[fd]); ofiles[fd] = NULL; + /* cppcheck-suppress memleak */ SL_IRETURN(SL_EMEM, _("sl_make_ticket")); } @@ -1779,6 +1849,7 @@ SL_TICKET sl_make_ticket (const char * ofile, int oline, (void) free (ofiles[fd]->path); (void) free (ofiles[fd]); ofiles[fd] = NULL; + /* cppcheck-suppress memleak */ SL_IRETURN(ticket, _("sl_make_ticket")); } @@ -1792,6 +1863,7 @@ SL_TICKET sl_make_ticket (const char * ofile, int oline, sl_strlcpy(ofiles[fd]->ofile, ofile, SL_OFILE_SIZE); ofiles[fd]->oline = oline; + /* cppcheck-suppress memleak */ SL_IRETURN(ticket, _("sl_make_ticket")); } @@ -2376,6 +2448,7 @@ int sl_close (SL_TICKET ticket) TPT((0, FIL__, __LINE__, _("msg=<Error fclosing file.>, fd=<%d>, err=<%s>\n"), fd, strerror(errno))); + SL_IRETURN(SL_ECLOSE, _("sl_close")); } } else @@ -2385,6 +2458,7 @@ int sl_close (SL_TICKET ticket) TPT((0, FIL__, __LINE__, _("msg=<Error closing file.>, fd=<%d>, err=<%s>\n"), fd, strerror(errno))); + SL_IRETURN(SL_ECLOSE, _("sl_close")); } } @@ -2559,8 +2633,8 @@ int sl_read_timeout_prep (SL_TICKET ticket) } -int sl_read_timeout_fd (int fd, void * buf_in, size_t count, - int timeout, int is_nonblocking) +static int sl_read_timeout_fd_int (int fd, void * buf_in, size_t count, + int timeout, int is_nonblocking, int once) { int sflags = 0; fd_set readfds; @@ -2609,7 +2683,7 @@ int sl_read_timeout_fd (int fd, void * buf_in, size_t count, { bytes += byteread; count -= byteread; buf += byteread; - if (count == 0) + if (count == 0 || once == S_TRUE) break; } else if (byteread == 0) @@ -2693,6 +2767,18 @@ int sl_read_timeout_fd (int fd, void * buf_in, size_t count, return ((int) bytes); } +int sl_read_timeout_fd (int fd, void * buf_in, size_t count, + int timeout, int is_nonblocking) +{ + return sl_read_timeout_fd_int (fd, buf_in, count, timeout, is_nonblocking, S_FALSE); +} + +int sl_read_timeout_fd_once (int fd, void * buf_in, size_t count, + int timeout, int is_nonblocking) +{ + return sl_read_timeout_fd_int (fd, buf_in, count, timeout, is_nonblocking, S_TRUE); +} + int sl_read_timeout (SL_TICKET ticket, void * buf_in, size_t count, int timeout, int is_nonblocking) { |