diff --git a/array/array_example.c b/array/array_example.c index f8a3c4a..7bad9d9 100644 --- a/array/array_example.c +++ b/array/array_example.c @@ -5,6 +5,7 @@ int main(int argc, char *argv[]) { int *p; + int val; sc_array_create(p, 0); @@ -24,6 +25,10 @@ int main(int argc, char *argv[]) printf("Elem = %d \n", p[i]); } + sc_array_foreach(p, val) { + printf("Elem = %d \n", val); + } + sc_array_destroy(p); return 0; diff --git a/array/array_test.c b/array/array_test.c index f11aa25..d082173 100644 --- a/array/array_test.c +++ b/array/array_test.c @@ -204,6 +204,11 @@ void fail_test() sc_array_remove_unordered(arr, 0); assert(arr[0] == 4); assert(sc_array_size(arr) == 4); + sc_array_clear(arr); + assert(sc_array_size(arr) == 0); + sc_array_add(arr, 10); + assert(sc_array_size(arr) == 1); + assert(arr[0] == 10); sc_array_destroy(arr); } diff --git a/array/sc_array.c b/array/sc_array.c index 47f3db6..5218bbd 100644 --- a/array/sc_array.c +++ b/array/sc_array.c @@ -24,9 +24,6 @@ #include "sc_array.h" -#include -#include - #ifndef SC_SIZE_MAX #define SC_SIZE_MAX SIZE_MAX #endif diff --git a/buffer/buf_test.c b/buffer/buf_test.c index dc81be5..4cb9a11 100644 --- a/buffer/buf_test.c +++ b/buffer/buf_test.c @@ -129,6 +129,18 @@ void test1() buf = sc_buf_wrap(text, sizeof(text), SC_BUF_REF); sc_buf_put_text(&buf, "test %d test %s", 1, "test"); assert(strcmp(sc_buf_rbuf(&buf), "test 1 test test") == 0); + sc_buf_term(&buf); + + buf = sc_buf_wrap(text, sizeof(text), SC_BUF_REF); + sc_buf_put_str(&buf, "1test"); + sc_buf_put_str(&buf, "2test"); + + buf2 = sc_buf_wrap(sc_buf_rbuf(&buf), sc_buf_size(&buf), SC_BUF_READ); + assert(sc_buf_size(&buf2) == sc_buf_size(&buf)); + assert(strcmp(sc_buf_get_str(&buf2), "1test") == 0); + assert(strcmp(sc_buf_get_str(&buf2), "2test") == 0); + sc_buf_term(&buf); + sc_buf_term(&buf2); } void test2() diff --git a/buffer/sc_buf.c b/buffer/sc_buf.c index f3191d7..a28b2ff 100644 --- a/buffer/sc_buf.c +++ b/buffer/sc_buf.c @@ -26,8 +26,6 @@ #include #include -#include - #define sc_buf_min(a, b) ((a) > (b) ? (b) : (a)) @@ -56,7 +54,7 @@ struct sc_buf sc_buf_wrap(void *data, uint32_t len, int flags) .limit = flags & SC_BUF_REF ? len : UINT32_MAX, .wpos = flags & SC_BUF_DATA ? len : 0, .rpos = 0, - .ref = flags & SC_BUF_REF, + .ref = (bool) (flags & SC_BUF_REF), .error = 0}; return buf; @@ -281,7 +279,7 @@ static uint32_t sc_buf_peek_64_pos(struct sc_buf *buf, uint32_t pos, uint64_t *v return sizeof(*val); } -static uint32_t sc_buf_set_8_pos(struct sc_buf *buf, uint32_t pos, uint8_t *val) +static uint32_t sc_buf_set_8_pos(struct sc_buf *buf, uint32_t pos, const uint8_t *val) { if (buf->error != 0 || (pos + sizeof(*val) > buf->cap)) { buf->error |= SC_BUF_CORRUPT; @@ -293,7 +291,7 @@ static uint32_t sc_buf_set_8_pos(struct sc_buf *buf, uint32_t pos, uint8_t *val) return sizeof(*val); } -static uint32_t sc_buf_set_16_pos(struct sc_buf *buf, uint32_t pos, uint16_t *val) +static uint32_t sc_buf_set_16_pos(struct sc_buf *buf, uint32_t pos, const uint16_t *val) { unsigned char* p; @@ -304,15 +302,15 @@ static uint32_t sc_buf_set_16_pos(struct sc_buf *buf, uint32_t pos, uint16_t *va p = &buf->mem[pos]; - p[0] = *val >> 0; - p[1] = *val >> 8; - p[2] = *val >> 16; - p[3] = *val >> 24; + p[0] = (unsigned char) (*val >> 0); + p[1] = (unsigned char) (*val >> 8); + p[2] = (unsigned char) (*val >> 16); + p[3] = (unsigned char) (*val >> 24); return sizeof(*val); } -static uint32_t sc_buf_set_32_pos(struct sc_buf *buf, uint32_t pos, uint32_t *val) +static uint32_t sc_buf_set_32_pos(struct sc_buf *buf, uint32_t pos, const uint32_t *val) { unsigned char* p; @@ -323,15 +321,15 @@ static uint32_t sc_buf_set_32_pos(struct sc_buf *buf, uint32_t pos, uint32_t *va p = &buf->mem[pos]; - p[0] = *val >> 0; - p[1] = *val >> 8; - p[2] = *val >> 16; - p[3] = *val >> 24; + p[0] = (unsigned char) (*val >> 0); + p[1] = (unsigned char) (*val >> 8); + p[2] = (unsigned char) (*val >> 16); + p[3] = (unsigned char) (*val >> 24); return sizeof(*val); } -static uint32_t sc_buf_set_64_pos(struct sc_buf *buf, uint32_t pos, uint64_t *val) +static uint32_t sc_buf_set_64_pos(struct sc_buf *buf, uint32_t pos, const uint64_t *val) { unsigned char* p; @@ -342,14 +340,14 @@ static uint32_t sc_buf_set_64_pos(struct sc_buf *buf, uint32_t pos, uint64_t *va p = &buf->mem[pos]; - p[0] = *val >> 0; - p[1] = *val >> 8; - p[2] = *val >> 16; - p[3] = *val >> 24; - p[4] = *val >> 32; - p[5] = *val >> 40; - p[6] = *val >> 48; - p[7] = *val >> 56; + p[0] = (unsigned char) (*val >> 0); + p[1] = (unsigned char) (*val >> 8); + p[2] = (unsigned char) (*val >> 16); + p[3] = (unsigned char) (*val >> 24); + p[4] = (unsigned char) (*val >> 32); + p[5] = (unsigned char) (*val >> 40); + p[6] = (unsigned char) (*val >> 48); + p[7] = (unsigned char) (*val >> 56); return sizeof(*val); } @@ -581,11 +579,9 @@ void sc_buf_put_raw(struct sc_buf *buf, const void *ptr, uint32_t len) void sc_buf_put_bool(struct sc_buf *buf, bool val) { - sc_buf_put_8(buf, val); + sc_buf_put_8(buf, (uint8_t) val); } - - void sc_buf_put_8(struct sc_buf *buf, uint8_t val) { if (!sc_buf_reserve(buf, sizeof(val))) { @@ -595,8 +591,6 @@ void sc_buf_put_8(struct sc_buf *buf, uint8_t val) buf->wpos += sc_buf_set_8_pos(buf, buf->wpos, &val); } - - void sc_buf_put_16(struct sc_buf *buf, uint16_t val) { if (!sc_buf_reserve(buf, sizeof(val))) { @@ -606,8 +600,6 @@ void sc_buf_put_16(struct sc_buf *buf, uint16_t val) buf->wpos += sc_buf_set_16_pos(buf, buf->wpos, &val); } - - void sc_buf_put_32(struct sc_buf *buf, uint32_t val) { if (!sc_buf_reserve(buf, sizeof(val))) { @@ -638,28 +630,34 @@ void sc_buf_put_double(struct sc_buf *buf, double val) void sc_buf_put_str(struct sc_buf *buf, const char *str) { - uint32_t size; + size_t size; if (str == NULL) { - sc_buf_put_32(buf, -1); + sc_buf_put_32(buf, UINT32_MAX); return; } size = strlen(str); + if (size >= UINT32_MAX) { + buf->error |= SC_BUF_CORRUPT; + return; + } - sc_buf_put_32(buf, size); - sc_buf_put_raw(buf, str, size + sc_buf_8_len('\0')); + sc_buf_put_32(buf, (uint32_t) size); + sc_buf_put_raw(buf, str, (uint32_t) size + sc_buf_8_len('\0')); } void sc_buf_put_str_len(struct sc_buf *buf, const char *str, int len) { + assert(len > 0); + if (str == NULL) { - sc_buf_put_32(buf, -1); + sc_buf_put_32(buf, UINT32_MAX); return; } - sc_buf_put_32(buf, len); - sc_buf_put_raw(buf, str, len); + sc_buf_put_32(buf, (uint32_t) len); + sc_buf_put_raw(buf, str, (uint32_t) len); sc_buf_put_8(buf, '\0'); } @@ -687,7 +685,7 @@ void sc_buf_put_fmt(struct sc_buf *buf, const char *fmt, ...) } if (rc >= quota) { - if (!sc_buf_reserve(buf, rc)) { + if (!sc_buf_reserve(buf, (uint32_t) rc)) { return; } @@ -705,7 +703,7 @@ void sc_buf_put_fmt(struct sc_buf *buf, const char *fmt, ...) } - sc_buf_set_32_at(buf, pos, rc); + sc_buf_set_32_at(buf, pos, (uint32_t) rc); sc_buf_mark_write(buf, rc + sc_buf_32_len(0) + sc_buf_8_len('\0')); } @@ -728,7 +726,7 @@ void sc_buf_put_text(struct sc_buf *buf, const char *fmt, ...) } if (rc >= quota) { - if (!sc_buf_reserve(buf, rc)) { + if (!sc_buf_reserve(buf, (uint32_t) rc)) { sc_buf_set_wpos(buf, pos); return; } diff --git a/buffer/sc_buf.h b/buffer/sc_buf.h index a8f3096..d170836 100644 --- a/buffer/sc_buf.h +++ b/buffer/sc_buf.h @@ -361,16 +361,17 @@ void sc_buf_put_raw(struct sc_buf *buf, const void *ptr, uint32_t len); */ // clang-format off -static inline uint32_t sc_buf_bool_len(bool val) { return 1;} -static inline uint32_t sc_buf_8_len(uint8_t val) {return 1;} -static inline uint32_t sc_buf_16_len(uint16_t val){return 2;} -static inline uint32_t sc_buf_32_len(uint32_t val){return 4;} -static inline uint32_t sc_buf_64_len(uint64_t val){return 8;} -static inline uint32_t sc_buf_double_len(double val){return 8;} +static inline uint32_t sc_buf_bool_len(bool val) {(void) val; return 1;} +static inline uint32_t sc_buf_8_len(uint8_t val) {(void) val; return 1;} +static inline uint32_t sc_buf_16_len(uint16_t val){(void) val; return 2;} +static inline uint32_t sc_buf_32_len(uint32_t val){(void) val; return 4;} +static inline uint32_t sc_buf_64_len(uint64_t val){(void) val; return 8;} +static inline uint32_t sc_buf_double_len(double val){(void) val; return 8;} // clang-format on static inline uint32_t sc_buf_blob_len(void *ptr, uint32_t len) { + (void) ptr; assert(len <= INT32_MAX - 4); return len + sc_buf_32_len(len); @@ -378,13 +379,15 @@ static inline uint32_t sc_buf_blob_len(void *ptr, uint32_t len) static inline uint32_t sc_buf_str_len(const char *str) { + const uint32_t bytes = sc_buf_32_len(UINT32_MAX) + sc_buf_8_len('\0'); + if (str == NULL) { - return sc_buf_32_len(-1); + return sc_buf_32_len(UINT32_MAX); } assert(strlen(str) <= INT32_MAX - 5); - return sc_buf_32_len(-1) + strlen(str) + sc_buf_8_len('\0'); + return bytes + (uint32_t) strlen(str); } #endif diff --git a/crc32/sc_crc32.c b/crc32/sc_crc32.c index 140addb..4e2c5dd 100644 --- a/crc32/sc_crc32.c +++ b/crc32/sc_crc32.c @@ -34,9 +34,9 @@ 1.2 2020 Added gcc intrinsics, fixed alignment issues. */ -#include +#include "sc_crc32.h" + #include -#include /* CRC-32C (iSCSI) polynomial in reversed bit order. */ #define CRC32_POLY 0x82f63b78 diff --git a/ini/sc_ini.c b/ini/sc_ini.c index 6672f4f..b432a76 100644 --- a/ini/sc_ini.c +++ b/ini/sc_ini.c @@ -79,7 +79,7 @@ static char *trim_bom(char *str) } int sc_ini_parse(void *arg, sc_ini_on_item on_item, void *arg1, - char *(*next_line)(void *arg, char *buf, size_t size)) + char *(*next_line)(void *, char *, size_t)) { int rc = 0, line = 0; char buf[SC_INI_MAX_LINE_LEN]; @@ -129,7 +129,7 @@ int sc_ini_parse(void *arg, sc_ini_on_item on_item, void *arg1, static char *file_next_line(void *p, char *buf, size_t size) { - return fgets(buf, size, (FILE *) p); + return fgets(buf, (int) size, (FILE *) p); } static char *string_next_line(void *p, char *buf, size_t size) diff --git a/logger/sc_log.c b/logger/sc_log.c index c508446..cc5058c 100644 --- a/logger/sc_log.c +++ b/logger/sc_log.c @@ -26,7 +26,6 @@ #include #include -#include #include #ifndef thread_local @@ -236,7 +235,7 @@ int sc_log_set_file(const char *prev_file, const char *current_file) goto error; } - sc_log.file_size = size; + sc_log.file_size = (size_t) size; sc_log.fp = fp; goto out; diff --git a/logger/sc_log.h b/logger/sc_log.h index d300ed6..7c47c80 100644 --- a/logger/sc_log.h +++ b/logger/sc_log.h @@ -42,7 +42,7 @@ enum sc_log_level // clang-format off const static struct sc_log_level_pair { - const int id; + const enum sc_log_level id; const char *str; } sc_log_levels[] = { {SC_LOG_DEBUG, "DEBUG"}, @@ -135,7 +135,7 @@ int sc_log_set_callback(sc_log_callback cb, void *arg); int sc_log_log(enum sc_log_level level, const char *fmt, ...); /** - * Max file size to rotate. + * Max file size to rotate, should not be more than 4 GB. */ #define SC_LOG_FILE_SIZE (2 * 1024 * 1024) @@ -144,6 +144,7 @@ int sc_log_log(enum sc_log_level level, const char *fmt, ...); * in the log line. */ #ifdef SC_LOG_PRINT_FILE_NAME + #define sc_log_ap(fmt, ...) \ "(%s:%d) " fmt, strrchr("/" __FILE__, '/') + 1, __LINE__, \ __VA_ARGS__ @@ -151,7 +152,6 @@ int sc_log_log(enum sc_log_level level, const char *fmt, ...); #define sc_log_ap(fmt, ...) fmt, __VA_ARGS__ #endif - /** * Printf-style format * e.g diff --git a/map/sc_map.c b/map/sc_map.c index de502f2..1872b99 100644 --- a/map/sc_map.c +++ b/map/sc_map.c @@ -1,8 +1,5 @@ #include "sc_map.h" -#include -#include - #ifndef SC_SIZE_MAX #define SC_SIZE_MAX UINT32_MAX #endif diff --git a/map/sc_map.h b/map/sc_map.h index db8c23d..84137c3 100644 --- a/map/sc_map.h +++ b/map/sc_map.h @@ -75,8 +75,8 @@ uint32_t sc_map_size_##name(struct sc_map_##name *map); \ void sc_map_clear_##name(struct sc_map_##name *map); \ bool sc_map_put_##name(struct sc_map_##name *map, K key, V val); \ - bool sc_map_get_##name(struct sc_map_##name *map, K key, V *value); \ - bool sc_map_del_##name(struct sc_map_##name *map, K key, V *value); + bool sc_map_get_##name(struct sc_map_##name *map, K key, V *val); \ + bool sc_map_del_##name(struct sc_map_##name *map, K key, V *val); #define sc_map_foreach(map, K, V) \ for (uint32_t __i = 0, __b = 0; __i < (map)->cap; __i++) \ diff --git a/math/math_test.c b/math/math_test.c index 74e953a..92181f9 100644 --- a/math/math_test.c +++ b/math/math_test.c @@ -14,6 +14,8 @@ void test1() char *t; char buf[32]; + assert(sc_math_min(199, 299) == 199); + assert(sc_math_max(199, 299) == 299); assert(sc_math_is_pow2(0) == false); x = sc_math_to_pow2(0); diff --git a/math/sc_math.h b/math/sc_math.h index f19d159..c01eb54 100644 --- a/math/sc_math.h +++ b/math/sc_math.h @@ -28,8 +28,8 @@ #include #include -#define sc_math_max(a, b) ((a) > (b) ? (a) : (b)) -#define sc_math_min(a, b) ((a) > (b) ? (b) : (a)) +#define sc_math_max(a, b) (((a) > (b)) ? (a) : (b)) +#define sc_math_min(a, b) (((a) > (b)) ? (b) : (a)) /** * @param num num diff --git a/memory-map/sc_mmap.c b/memory-map/sc_mmap.c index e608bfa..553e3bf 100644 --- a/memory-map/sc_mmap.c +++ b/memory-map/sc_mmap.c @@ -2,8 +2,6 @@ #include "sc_mmap.h" #include -#include -#include #include #include diff --git a/option/sc_option.c b/option/sc_option.c index b9a8fdf..9dd4597 100644 --- a/option/sc_option.c +++ b/option/sc_option.c @@ -29,7 +29,7 @@ char sc_option_at(struct sc_option *opt, int index, char **value) { char id = '?'; - int len; + size_t len; char *pos; const char *curr, *name; @@ -59,7 +59,7 @@ char sc_option_at(struct sc_option *opt, int index, char **value) for (int i = 0; i < opt->count; i++) { curr = opt->argv[index] + 2; // Skip '--' name = opt->options[i].name; - len = (int) (pos - curr); + len = (pos - curr); if (name == NULL) { continue; diff --git a/queue/queue_test.c b/queue/queue_test.c index 2e3d9a1..08bc568 100644 --- a/queue/queue_test.c +++ b/queue/queue_test.c @@ -151,6 +151,16 @@ void test1(void) sc_queue_destroy(p); sc_queue_destroy(p); + + assert(sc_queue_create(p, 0) == true); + sc_queue_add_first(p, 100); + sc_queue_add_first(p, 200); + sc_queue_add_first(p, 300); + sc_queue_add_first(p, 400); + sc_queue_add_first(p, 500); + assert(sc_queue_at(p, 0) == 500); + assert(sc_queue_at(p, 4) == 100); + sc_queue_destroy(p); } int main(int argc, char *argv[]) diff --git a/queue/sc_queue.c b/queue/sc_queue.c index f837040..20108cf 100644 --- a/queue/sc_queue.c +++ b/queue/sc_queue.c @@ -24,10 +24,8 @@ #include "sc_queue.h" -#include - #ifndef SC_SIZE_MAX -#define SC_SIZE_MAX SIZE_MAX + #define SC_SIZE_MAX SIZE_MAX #endif #define SC_MAX_CAP ((SC_SIZE_MAX - sizeof(struct sc_queue)) / 2ul) @@ -123,9 +121,9 @@ bool sc_queue_expand(void **q, size_t elem_size) * | | * Step 0 : | 2 | 3 | - | 1 | // tmp->cap : 4 * Step 1 : | 2 | 3 | - | 1 | - | - | - | - | // realloc - * Step 2 : | 2 | 3 | - | 1 | 1 | - | - | - | // mempcy + * Step 2 : | 2 | 3 | - | 1 | 1 | - | - | - | // memcpy * Step 3 : | 2 | 2 | 3 | 1 | 1 | - | - | - | // memmove - * Step 4 : | 1 | 2 | 3 | 1 | 1 | - | - | - | // mempcy + * Step 4 : | 1 | 2 | 3 | 1 | 1 | - | - | - | // memcpy * Step 5 : | 1 | 2 | 3 | - | - | - | - | - | // tmp->last = cap - 1; * | | * first last diff --git a/queue/sc_queue.h b/queue/sc_queue.h index e6e9be0..a6a987f 100644 --- a/queue/sc_queue.h +++ b/queue/sc_queue.h @@ -154,14 +154,14 @@ bool sc_queue_expand(void **q, size_t elem_size); #define sc_queue_next(q, i) (((i) + 1) & (sc_queue_meta(q)->cap - 1)) /** - * Returns element at 'i'th position, so regular loops are possible : + * Returns element at index 'i', so regular loops are possible : * * for (size_t i = 0; i < sc_queue_size(q); i++) { * printf("%d" \n, sc_queue_at(q, i)); * } * * @param q Queue pointer - * @return element at 'i'th position + * @return element at index i */ #define sc_queue_at(q, i) \ (q)[((sc_queue_meta(q)->first) + (i)) & (sc_queue_cap(q) - 1)] diff --git a/rc4/sc_rc4.c b/rc4/sc_rc4.c index c3ca67b..ce94077 100644 --- a/rc4/sc_rc4.c +++ b/rc4/sc_rc4.c @@ -25,7 +25,6 @@ #include "sc_rc4.h" #include -#include void sc_rc4_init(struct sc_rc4 *rc4, const unsigned char rand[256]) { diff --git a/signal/sc_signal.c b/signal/sc_signal.c index e511291..b89a54a 100644 --- a/signal/sc_signal.c +++ b/signal/sc_signal.c @@ -24,17 +24,14 @@ #include "sc_signal.h" -#include -#include #include -#include #include #include #include #include #if defined(_WIN32) -#include + #include volatile SOCKET sc_signal_shutdown_fd; #else volatile sig_atomic_t sc_signal_shutdown_fd; @@ -63,6 +60,8 @@ volatile sig_atomic_t sc_signal_will_shutdown; (size) == 1 ? va_arg(va, int) : \ 0 +#define PSIZE sizeof(void*) == sizeof(unsigned long long) ? 3 : 2 + int sc_signal_vsnprintf(char *buf, size_t size, const char *fmt, va_list va) { size_t len; @@ -98,15 +97,15 @@ int sc_signal_vsnprintf(char *buf, size_t size, const char *fmt, va_list va) uint64_t u64 = get_uint(va, pos - orig); do { - digits[31 - (len++)] = (char)('0' + (u64 % 10)); + digits[31 - (len++)] = (char) ('0' + (u64 % 10)); } while (u64 /= 10UL); } else if (*pos == 'd') { int64_t i64 = get_int(va, pos - orig); - uint64_t u64 = i64 < 0 ? -i64 : i64; + uint64_t u64 = (uint64_t) (i64 < 0 ? -i64 : i64); do { - digits[31 - (len++)] = (char)('0' + (char)(u64 % 10)); + digits[31 - (len++)] = (char) ('0' + (char) (u64 % 10)); } while (u64 /= 10UL); if (i64 < 0) { @@ -119,10 +118,9 @@ int sc_signal_vsnprintf(char *buf, size_t size, const char *fmt, va_list va) str = &digits[32 - len]; break; case 'p': { - char *arr = "0123456789abcdef"; + const char *arr = "0123456789abcdef"; len = 0; - int s = (sizeof(void *) == sizeof(unsigned long long)) ? 3 : 2; - uint64_t u64 = get_uint(va, s); + uint64_t u64 = get_uint(va, PSIZE); do { digits[31 - (len++)] = arr[u64 % 16]; @@ -173,15 +171,15 @@ int sc_signal_snprintf(char *buf, size_t size, const char *fmt, ...) #if defined(_WIN32) -#define WIN32_LEAN_AND_MEAN + #define WIN32_LEAN_AND_MEAN -#include -#include -#include -#include + #include + #include + #include + #include -#pragma warning(disable : 4996) -#pragma comment(lib, "Ws2_32.lib") + #pragma warning(disable : 4996) + #pragma comment(lib, "Ws2_32.lib") BOOL WINAPI sc_console_handler(DWORD type) { @@ -296,7 +294,7 @@ int sc_signal_init() #else -// clang-format off + // clang-format off #include #ifdef HAVE_BACKTRACE @@ -304,51 +302,51 @@ int sc_signal_init() static void *sc_instruction(ucontext_t *uc) { - void* insp = NULL; + void* p = NULL; #if defined(__APPLE__) && defined(MAC_OS_X_VERSION_10_6) #if defined(_STRUCT_X86_THREAD_STATE64) && !defined(__i386__) - insp = (void *) uc->uc_mcontext->__ss.__rip; + p = (void *) uc->uc_mcontext->__ss.__rip; #elif defined(__i386__) - insp = (void *) uc->uc_mcontext->__ss.__eip; + p = (void *) uc->uc_mcontext->__ss.__eip; #else - insp = (void *) arm_thread_state64_get_pc(uc->uc_mcontext->__ss); + p = (void *) arm_thread_state64_get_pc(uc->uc_mcontext->__ss); #endif #elif defined(__linux__) #if defined(__i386__) || ((defined(__x86_64__)) && defined(__ILP32__)) - insp = (void *) uc->uc_mcontext.gregs[REG_EIP]; + p = (void *) uc->uc_mcontext.gregs[REG_EIP]; #elif defined(__x86_64__) - insp = (void *) uc->uc_mcontext.gregs[REG_RIP]; + p = (void *) uc->uc_mcontext.gregs[REG_RIP]; #elif defined(__ia64__) - insp = (void *) uc->uc_mcontext.sc_ip; + p = (void *) uc->uc_mcontext.sc_ip; #elif defined(__arm__) - insp = (void *) uc->uc_mcontext.arm_pc; + p = (void *) uc->uc_mcontext.arm_pc; #elif defined(__aarch64__) - insp = (void *) uc->uc_mcontext.pc; + p = (void *) uc->uc_mcontext.pc; #endif #elif defined(__FreeBSD__) #if defined(__i386__) - insp = (void *) uc->uc_mcontext.mc_eip; + p = (void *) uc->uc_mcontext.mc_eip; #elif defined(__x86_64__) - insp = (void *) uc->uc_mcontext.mc_rip; + p = (void *) uc->uc_mcontext.mc_rip; #endif #elif defined(__OpenBSD__) #if defined(__i386__) - insp = (void *) uc->sc_eip; + p = (void *) uc->sc_eip; #elif defined(__x86_64__) - insp = (void *) uc->sc_rip; + p = (void *) uc->sc_rip; #endif #elif defined(__NetBSD__) #if defined(__i386__) - insp = (void *) uc->uc_mcontext.__gregs[_REG_EIP]; + p = (void *) uc->uc_mcontext.__gregs[_REG_EIP]; #elif defined(__x86_64__) - insp = (void *) uc->uc_mcontext.__gregs[_REG_RIP]; + p = (void *) uc->uc_mcontext.__gregs[_REG_RIP]; #endif #elif defined(__DragonFly__) - insp = (void *) uc->uc_mcontext.mc_rip; + p = (void *) uc->uc_mcontext.mc_rip; #endif - return insp; + return p; } #endif @@ -384,7 +382,7 @@ static void sc_signal_on_shutdown(int sig) if (sc_signal_shutdown_fd != -1) { sc_signal_log(fd, buf, sizeof(buf), "Sending shutdown command. \n"); - rc = write(sc_signal_shutdown_fd, (void *) &(int){1}, 1); + rc = (int) write(sc_signal_shutdown_fd, (void *) &(int){1}, 1); if (rc != 1) { sc_signal_log(fd, buf, sizeof(buf), "Failed to send shutdown command, " @@ -400,6 +398,8 @@ static void sc_signal_on_shutdown(int sig) static void sc_signal_on_fatal(int sig, siginfo_t *info, void *context) { + (void) info; + int fd = sc_signal_log_fd != -1 ? sc_signal_log_fd : STDERR_FILENO; char buf[4096], *sig_str; @@ -432,7 +432,7 @@ static void sc_signal_on_fatal(int sig, siginfo_t *info, void *context) sc_signal_log(fd, buf, sizeof(buf), "\n----------------- CRASH REPORT ---------------- \n"); -#ifdef HAVE_BACKTRACE + #ifdef HAVE_BACKTRACE void *caller = sc_instruction((ucontext_t *) context); int trace_size; void *trace[100]; @@ -441,7 +441,7 @@ static void sc_signal_on_fatal(int sig, siginfo_t *info, void *context) trace_size = backtrace(trace, 100); backtrace_symbols_fd(trace, trace_size, fd); -#endif + #endif sc_signal_log(fd, buf, sizeof(buf), "\n--------------- CRASH REPORT END -------------- \n"); @@ -498,5 +498,5 @@ void sc_signal_log(int fd, char *buf, size_t len, char *fmt, ...) written = sc_signal_vsnprintf(buf, len, fmt, args); va_end(args); - (void) write(fd, buf, written); + (void) write(fd, buf, (size_t) written); } diff --git a/signal/signal_test.c b/signal/signal_test.c index 2277afb..6d8d954 100644 --- a/signal/signal_test.c +++ b/signal/signal_test.c @@ -1,7 +1,6 @@ #include "sc_signal.h" #include -#include #include void test1() diff --git a/socket/sc_sock.c b/socket/sc_sock.c index e8db5c3..75b6045 100644 --- a/socket/sc_sock.c +++ b/socket/sc_sock.c @@ -45,6 +45,8 @@ #define SC_EINPROGRESS WSAEINPROGRESS #define SC_EINTR WSAEINTR +typedef int socklen_t; + static int sc_sock_err() { return WSAGetLastError(); @@ -77,7 +79,6 @@ int sc_sock_set_blocking(struct sc_sock *sock, bool blocking) #else #include #include - #include #include #include #include @@ -202,9 +203,9 @@ static int sc_sock_bind_unix(struct sc_sock *sock, const char *host) static int sc_sock_bind(struct sc_sock *sock, const char *host, const char *prt) { const int bf = SC_SOCK_BUF_SIZE; - const int sz = sizeof(bf); + const socklen_t sz = sizeof(bf); - int rc = 0, rv = 0; + int rc, rv = 0; struct addrinfo *servinfo = NULL; struct addrinfo hints = {.ai_family = sock->family, .ai_socktype = SOCK_STREAM}; @@ -351,9 +352,9 @@ int sc_sock_connect(struct sc_sock *sock, const char *dest_addr, const char *source_port) { const int bf = SC_SOCK_BUF_SIZE; - const int sz = sizeof(bf); + const socklen_t sz = sizeof(bf); - int rc = 0, rv = SC_SOCK_OK; + int rc, rv = SC_SOCK_OK; struct addrinfo inf = {.ai_family = AF_UNSPEC, .ai_socktype = SOCK_STREAM}; struct addrinfo *servinfo = NULL, *bindinfo = NULL, *p, *s; @@ -491,7 +492,7 @@ int sc_sock_send(struct sc_sock *sock, char *buf, int len, int flags) } retry: - n = send(sock->fdt.fd, buf, len, flags); + n = send(sock->fdt.fd, buf, (size_t) len, flags); if (n == SC_ERR) { int err = sc_sock_err(); if (err == SC_EINTR) { @@ -520,7 +521,7 @@ int sc_sock_recv(struct sc_sock *sock, char *buf, int len, int flags) } retry: - n = recv(sock->fdt.fd, buf, len, flags); + n = recv(sock->fdt.fd, buf, (size_t) len, flags); if (n == 0) { return SC_SOCK_ERROR; } else if (n == SC_ERR) { @@ -543,7 +544,7 @@ retry: int sc_sock_accept(struct sc_sock *sock, struct sc_sock *in) { const int bf = SC_SOCK_BUF_SIZE; - const int sz = sizeof(bf); + const socklen_t sz = sizeof(bf); int rc; sc_sock_int fd; @@ -613,7 +614,7 @@ const char *sc_sock_error(struct sc_sock *sock) } static const char *sc_sock_addr(struct sc_sock *sock, int af, void *cp, - char *buf, int len) + char *buf, socklen_t len) { const char *dest; @@ -628,7 +629,7 @@ static const char *sc_sock_addr(struct sc_sock *sock, int af, void *cp, static const char *sc_sock_print_storage(struct sc_sock *sock, struct sockaddr_storage *storage, - char *buf, int len) + char *buf, size_t len) { const char *dst; struct sockaddr_in *addr; @@ -664,7 +665,7 @@ static const char *sc_sock_print_storage(struct sc_sock *sock, return buf; } -const char *sc_sock_local_str(struct sc_sock *sock, char *buf, int len) +const char *sc_sock_local_str(struct sc_sock *sock, char *buf, size_t len) { int rc; struct sockaddr_storage st; @@ -680,7 +681,7 @@ const char *sc_sock_local_str(struct sc_sock *sock, char *buf, int len) return sc_sock_print_storage(sock, &st, buf, len); } -const char *sc_sock_remote_str(struct sc_sock *sock, char *buf, int len) +const char *sc_sock_remote_str(struct sc_sock *sock, char *buf, size_t len) { int rc; struct sockaddr_storage st; @@ -696,7 +697,7 @@ const char *sc_sock_remote_str(struct sc_sock *sock, char *buf, int len) return sc_sock_print_storage(sock, &st, buf, len); } -void sc_sock_print(struct sc_sock *sock, char *buf, int len) +void sc_sock_print(struct sc_sock *sock, char *buf, size_t len) { char l[128]; char r[128]; @@ -840,7 +841,7 @@ int sc_sock_pipe_init(struct sc_sock_pipe *p, int type) } p->fdt.type = type; - p->fdt.op = 0; + p->fdt.op = SC_SOCK_NONE; p->fdt.fd = p->fds[0]; return 0; @@ -865,7 +866,7 @@ int sc_sock_pipe_term(struct sc_sock_pipe *p) return rc; } -int sc_sock_pipe_write(struct sc_sock_pipe *p, void *data, int len) +int sc_sock_pipe_write(struct sc_sock_pipe *p, void *data, unsigned int len) { ssize_t n; char *b = data; @@ -885,7 +886,7 @@ retry: return n; } -int sc_sock_pipe_read(struct sc_sock_pipe *p, void *data, int len) +int sc_sock_pipe_read(struct sc_sock_pipe *p, void *data, unsigned int len) { ssize_t n; char *b = data; diff --git a/socket/sc_sock.h b/socket/sc_sock.h index 12e0c56..676fff2 100644 --- a/socket/sc_sock.h +++ b/socket/sc_sock.h @@ -101,9 +101,9 @@ int sc_sock_send(struct sc_sock* sock, char* buf, int len, int flags); int sc_sock_recv(struct sc_sock* sock, char* buf, int len, int flags); const char* sc_sock_error(struct sc_sock* sock); -const char *sc_sock_local_str(struct sc_sock *sock, char *buf, int len); -const char *sc_sock_remote_str(struct sc_sock *sock, char *buf, int len); -void sc_sock_print(struct sc_sock* sock, char* buf, int len); +const char *sc_sock_local_str(struct sc_sock *sock, char *buf, size_t len); +const char *sc_sock_remote_str(struct sc_sock *sock, char *buf, size_t len); +void sc_sock_print(struct sc_sock* sock, char* buf, size_t len); struct sc_sock_pipe @@ -114,8 +114,8 @@ struct sc_sock_pipe int sc_sock_pipe_init(struct sc_sock_pipe* pipe, int type); int sc_sock_pipe_term(struct sc_sock_pipe* pipe); -int sc_sock_pipe_write(struct sc_sock_pipe* pipe, void* data, int len); -int sc_sock_pipe_read(struct sc_sock_pipe* pipe, void* data, int len); +int sc_sock_pipe_write(struct sc_sock_pipe* pipe, void* data, unsigned int len); +int sc_sock_pipe_read(struct sc_sock_pipe* pipe, void* data, unsigned int len); #define sc_sock_on_error(...) diff --git a/socket/sock_test.c b/socket/sock_test.c index 09cf8ad..7a67d22 100644 --- a/socket/sock_test.c +++ b/socket/sock_test.c @@ -3,7 +3,6 @@ #include #include -#include #if defined(_WIN32) || defined(_WIN64) #include @@ -371,7 +370,6 @@ void test_pipe(void) } #ifdef SC_HAVE_WRAP -#include bool fail_close = false; int __real_close(int fd); diff --git a/string/sc_str.c b/string/sc_str.c index a110698..134537b 100644 --- a/string/sc_str.c +++ b/string/sc_str.c @@ -25,10 +25,10 @@ #include "sc_str.h" #include -#include -#include #include #include +#include +#include /** * String with 'length' at the start of the allocated memory @@ -65,7 +65,7 @@ char *sc_str_create(const char *str) return NULL; } - return sc_str_create_len(str, size); + return sc_str_create_len(str, (uint32_t) size); } char *sc_str_create_len(const char *str, uint32_t len) @@ -105,7 +105,7 @@ char *sc_str_create_va(const char *fmt, va_list va) return NULL; } - str->len = rc; + str->len = (uint32_t) rc; if (rc < sizeof(tmp)) { memcpy(str->buf, tmp, str->len + 1); @@ -276,7 +276,7 @@ bool sc_str_trim(char **str, const char *list) } if (start != *str || end != (*str) + len) { - start = sc_str_create_len(start, end - start); + start = sc_str_create_len(start, (uint32_t) (end - start)); if (start == NULL) { return false; } @@ -362,7 +362,7 @@ bool sc_str_replace(char **str, const char *replace, const char *with) return false; } - dest->len = size; + dest->len = (uint32_t) size; tmp = dest->buf; while (count--) { diff --git a/string/sc_str.h b/string/sc_str.h index 8f1f6a4..e8b7833 100644 --- a/string/sc_str.h +++ b/string/sc_str.h @@ -28,14 +28,13 @@ #include #include #include -#include #ifdef SC_HAVE_CONFIG_H -#include "sc_config.h" + #include "sc_config.h" #else -#define sc_str_malloc malloc -#define sc_str_realloc realloc -#define sc_str_free free + #define sc_str_malloc malloc + #define sc_str_realloc realloc + #define sc_str_free free #endif /** diff --git a/string/str_test.c b/string/str_test.c index 63ae6e8..86e8914 100644 --- a/string/str_test.c +++ b/string/str_test.c @@ -2,6 +2,7 @@ #include #include +#include #include #ifdef SC_HAVE_WRAP diff --git a/thread/sc_thread.c b/thread/sc_thread.c index 5fb34bf..7e77bf8 100644 --- a/thread/sc_thread.c +++ b/thread/sc_thread.c @@ -109,24 +109,24 @@ int sc_thread_stop(struct sc_thread *thread, void **ret) int sc_thread_start(struct sc_thread *thread, void *(*fn)(void *), void *arg) { int rc; - pthread_attr_t hndl; + pthread_attr_t attr; - rc = pthread_attr_init(&hndl); + rc = pthread_attr_init(&attr); if (rc != 0) { strncpy(thread->err, strerror(rc), sizeof(thread->err)); return -1; } // This may only fail with EINVAL. - pthread_attr_setdetachstate(&hndl, PTHREAD_CREATE_JOINABLE); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); - rc = pthread_create(&thread->id, &hndl, fn, arg); + rc = pthread_create(&thread->id, &attr, fn, arg); if (rc != 0) { strncpy(thread->err, strerror(rc), sizeof(thread->err)); } // This may only fail with EINVAL. - pthread_attr_destroy(&hndl); + pthread_attr_destroy(&attr); return rc; } diff --git a/thread/sc_thread.h b/thread/sc_thread.h index 74b3a02..2ef4c73 100644 --- a/thread/sc_thread.h +++ b/thread/sc_thread.h @@ -55,14 +55,14 @@ struct sc_thread void sc_thread_init(struct sc_thread* thread); /** - * @param thread thred + * @param thread thread * @return '0' on success, * '-1' on error, call 'sc_thread_err()' for error string. */ int sc_thread_term(struct sc_thread* thread); /** - * @param thread thred + * @param thread thread * @return '0' on success, * '-1' on error, call 'sc_thread_err()' for error string. */ diff --git a/time/sc_time.c b/time/sc_time.c index 7f46a3c..363af7e 100644 --- a/time/sc_time.c +++ b/time/sc_time.c @@ -98,8 +98,8 @@ uint64_t sc_time_mono_ms() rc = clock_gettime(CLOCK_MONOTONIC, &ts); assert(rc == 0); - return (int64_t)((int64_t) ts.tv_sec * 1000 + - (int64_t) ts.tv_nsec / 1000000); + return (uint64_t)((uint64_t) ts.tv_sec * 1000 + + (uint64_t) ts.tv_nsec / 1000000); #endif } diff --git a/timer/sc_timer.c b/timer/sc_timer.c index c226350..5d95ff6 100644 --- a/timer/sc_timer.c +++ b/timer/sc_timer.c @@ -119,7 +119,8 @@ static bool expand(struct sc_timer *timer) uint64_t sc_timer_add(struct sc_timer *timer, uint64_t timeout, uint64_t type, void *data) { - const size_t pos = (timeout / TICK + timer->head) & (WHEEL_COUNT - 1); + const uint32_t offset = (uint32_t)(timeout / TICK + timer->head); + const uint32_t pos = offset & (WHEEL_COUNT - 1); uint64_t id; uint32_t seq, index, wheel_pos; @@ -177,7 +178,7 @@ uint64_t sc_timer_timeout(struct sc_timer *timer, uint64_t timestamp, void *arg, const uint64_t time = timestamp - timer->timestamp; uint32_t wheel, base; uint32_t head = timer->head; - uint32_t wheels = min(time / TICK, WHEEL_COUNT); + uint32_t wheels = (uint32_t) (min(time / TICK, WHEEL_COUNT)); if (wheels == 0) { return min(TICK - time, TICK); diff --git a/url/sc_url.c b/url/sc_url.c index 523055d..36751df 100644 --- a/url/sc_url.c +++ b/url/sc_url.c @@ -38,14 +38,14 @@ struct sc_url *sc_url_create(const char *str) int diff; unsigned long val; size_t len, full_len, parts_len; - size_t scheme_len = 0, authority_len = 0, userinfo_len = 0; - size_t host_len = 0, port_len = 0, path_len = 0; + size_t scheme_len, authority_len = 0, userinfo_len = 0; + size_t host_len = 0, port_len = 0, path_len; size_t query_len = 0, fragment_len = 0; - char *scheme = "", *userinfo = "", *host = "", *port = ""; - char *path = "", *query = "", *fragment = ""; + char *scheme, *userinfo = "", *host = "", *port = ""; + char *path, *query = "", *fragment = ""; char *ptr, *dest, *parse_end; - char* pos = (char*) str; + char *pos = (char *) str; struct sc_url *url; if (str == NULL || (ptr = strstr(pos, ":")) == NULL) { @@ -126,10 +126,10 @@ struct sc_url *sc_url_create(const char *str) return NULL; } - len = snprintf(url->buf, full_len, s1, scheme_len, scheme, authority_len, - authority, userinfo_len, userinfo, host_len, host, port_len, - port, path_len, path, query_len, query, fragment_len, - fragment); + len = (size_t) snprintf(url->buf, full_len, s1, scheme_len, scheme, + authority_len, authority, userinfo_len, userinfo, + host_len, host, port_len, port, path_len, path, + query_len, query, fragment_len, fragment); assert(len == full_len - 1); dest = url->buf + strlen(url->buf) + 1; @@ -137,18 +137,19 @@ struct sc_url *sc_url_create(const char *str) scheme_len -= (scheme_len != 0); // Skip ":" userinfo_len -= (userinfo_len != 0); // Skip "@" diff = port_len != 0; - port_len -= diff; // Skip ":" - port += diff; // Skip ":" + port_len -= diff; // Skip ":" + port += diff; // Skip ":" diff = (query_len != 0); - query_len -= diff; // Skip "?" - query += diff; // Skip "?" + query_len -= diff; // Skip "?" + query += diff; // Skip "?" diff = (fragment_len != 0); - fragment_len -= diff; // Skip "#" - fragment += diff; // Skip "#" + fragment_len -= diff; // Skip "#" + fragment += diff; // Skip "#" - len = sprintf(dest, s2, scheme_len, scheme, 0, userinfo_len, userinfo, 0, - host_len, host, 0, port_len, port, 0, path_len, path, 0, - query_len, query, 0, fragment_len, fragment, 0); + len = (size_t) sprintf(dest, s2, scheme_len, scheme, 0, userinfo_len, + userinfo, 0, host_len, host, 0, port_len, port, 0, + path_len, path, 0, query_len, query, 0, fragment_len, + fragment, 0); assert(len == parts_len - 1); url->str = url->buf;