From d2facbce1e8faae2f1ea85c81ab27214fbc41aaa Mon Sep 17 00:00:00 2001 From: Shane Bryldt Date: Thu, 23 Mar 2017 13:30:31 -0600 Subject: [PATCH] FS-10167: More cleanup of libks, now compiles with no warnings or errors in VS2015, also fixed a bug in ks_pool.c related to logging prefix memory bounds checks. A few warnings had to be disabled entirely due to the issues coming from openssl. --- libs/libks/libks.vcxproj | 7 ++---- libs/libks/libks.vcxproj.filters | 9 -------- libs/libks/src/ks.c | 9 ++++++-- libs/libks/src/ks_log.c | 6 ++--- libs/libks/src/ks_pool.c | 2 -- libs/libks/src/ks_ssl.c | 2 +- libs/libks/src/kws.c | 39 ++++++++++++++++---------------- 7 files changed, 33 insertions(+), 41 deletions(-) diff --git a/libs/libks/libks.vcxproj b/libs/libks/libks.vcxproj index ee02d02e99..759e8c5167 100644 --- a/libs/libks/libks.vcxproj +++ b/libs/libks/libks.vcxproj @@ -97,7 +97,7 @@ %(AdditionalIncludeDirectories) true false - 4711;4574;4100;4127;4668;4255;4706;4710;4820;4090 + 4711;4574;4100;4127;4668;4255;4706;4710;4820;4090;4702 MachineX86 @@ -132,7 +132,7 @@ EnableAllWarnings true false - 4711;4574;4100;4127;4668;4255;4706;4710;4820;4090 + 4711;4574;4100;4127;4668;4255;4706;4710;4820;4090;4702 Windows @@ -158,8 +158,6 @@ - - @@ -194,7 +192,6 @@ - diff --git a/libs/libks/libks.vcxproj.filters b/libs/libks/libks.vcxproj.filters index 630ca2aed3..8e710f64f0 100644 --- a/libs/libks/libks.vcxproj.filters +++ b/libs/libks/libks.vcxproj.filters @@ -84,12 +84,6 @@ Source Files - - Source Files - - - Source Files - Source Files @@ -167,9 +161,6 @@ Header Files - - Header Files - Header Files diff --git a/libs/libks/src/ks.c b/libs/libks/src/ks.c index 656714e89b..ee9db3dfbe 100644 --- a/libs/libks/src/ks.c +++ b/libs/libks/src/ks.c @@ -63,8 +63,13 @@ KS_DECLARE(ks_status_t) ks_global_set_cleanup(ks_pool_cleanup_fn_t fn, void *arg KS_DECLARE(ks_status_t) ks_init(void) { - - srand(getpid() * (intptr_t)&pool + time(NULL)); + unsigned int pid = 0; +#ifdef __WINDOWS__ + pid = _getpid(); +#else + pid = getpid(); +#endif + srand(pid * (unsigned int)(intptr_t)&pool + (unsigned int)time(NULL)); ks_ssl_init_ssl_locks(); ks_global_pool(); ks_rng_init(); diff --git a/libs/libks/src/ks_log.c b/libs/libks/src/ks_log.c index 1d9f2a12eb..b8642122c7 100644 --- a/libs/libks/src/ks_log.c +++ b/libs/libks/src/ks_log.c @@ -110,7 +110,7 @@ static void default_logger(const char *file, const char *func, int line, int lev used += snprintf(buf + used - 1, sizeof(buf) - used, "#%d ", (int32_t)ks_thread_self_id()); } if (ks_log_prefix & KS_LOG_PREFIX_FILE) { - used += snprintf(buf + used - 1, sizeof(buf) - used, fp); + used += snprintf(buf + used - 1, sizeof(buf) - used, "%s", fp); if (ks_log_prefix & KS_LOG_PREFIX_LINE) { used += snprintf(buf + used - 1, sizeof(buf) - used, ":%d", line); } @@ -120,10 +120,10 @@ static void default_logger(const char *file, const char *func, int line, int lev used += snprintf(buf + used - 1, sizeof(buf) - used, "%s() ", func); } - used += snprintf(buf + used - 1, sizeof(buf) - used, data); + used += snprintf(buf + used - 1, sizeof(buf) - used, "%s", data); //fprintf(stderr, "[%s] %s:%d %s() %s", LEVEL_NAMES[level], fp, line, func, data); - fprintf(stderr, buf); + fprintf(stderr, "%s", buf); free(data); } diff --git a/libs/libks/src/ks_pool.c b/libs/libks/src/ks_pool.c index 49f18d16c3..d52af847e7 100644 --- a/libs/libks/src/ks_pool.c +++ b/libs/libks/src/ks_pool.c @@ -953,7 +953,6 @@ static void *alloc_mem(ks_pool_t *mp_p, const unsigned long byte_size, ks_status prefix->refs++; if (mp_p->mp_log_func != NULL) { - alloc_prefix_t *prefix = (alloc_prefix_t *) ((char *) addr - PREFIX_SIZE); mp_p->mp_log_func(mp_p, KS_POOL_FUNC_INCREF, prefix->size, prefix->refs, NULL, addr, 0); } @@ -1728,7 +1727,6 @@ KS_DECLARE(void *) ks_pool_ref_ex(ks_pool_t *mp_p, void *addr, ks_status_t *erro prefix->refs++; if (mp_p->mp_log_func != NULL) { - alloc_prefix_t *prefix = (alloc_prefix_t *) ((char *) addr - PREFIX_SIZE); mp_p->mp_log_func(mp_p, KS_POOL_FUNC_INCREF, prefix->size, prefix->refs, NULL, addr, 0); } diff --git a/libs/libks/src/ks_ssl.c b/libs/libks/src/ks_ssl.c index 763bfd3051..984d18461e 100644 --- a/libs/libks/src/ks_ssl.c +++ b/libs/libks/src/ks_ssl.c @@ -50,7 +50,7 @@ static inline void ks_ssl_lock_callback(int mode, int type, char *file, int line static inline unsigned long ks_ssl_thread_id(void) { - return (unsigned long) ks_thread_self(); + return ks_thread_self_id(); } KS_DECLARE(void) ks_ssl_init_ssl_locks(void) diff --git a/libs/libks/src/kws.c b/libs/libks/src/kws.c index c981917173..bd4e7ec5d5 100644 --- a/libs/libks/src/kws.c +++ b/libs/libks/src/kws.c @@ -97,6 +97,7 @@ static int cheezy_get_var(char *data, char *name, char *buf, ks_size_t buflen) /* the old way didnt make sure that variable values were used for the name hunt * and didnt ensure that only a full match of the variable name was used */ + ks_assert(buflen > 0); do { if(!strncmp(p,name,strlen(name)) && *(p+strlen(name))==':') break; @@ -120,11 +121,11 @@ static int cheezy_get_var(char *data, char *name, char *buf, ks_size_t buflen) } if (v && e) { - int cplen; + size_t cplen; ks_size_t len = e - v; if (len > buflen - 1) { - cplen = buflen -1; + cplen = buflen - 1; } else { cplen = len; } @@ -277,7 +278,7 @@ static int ws_server_handshake(kws_t *kws) } } - if (bytes < 0 || bytes > kws->buflen -1) { + if (bytes < 0 || ((ks_size_t)bytes) > kws->buflen - 1) { goto err; } @@ -294,7 +295,7 @@ static int ws_server_handshake(kws_t *kws) goto err; } - kws->uri = ks_pool_alloc(kws->pool, (e-p) + 1); + kws->uri = ks_pool_alloc(kws->pool, (unsigned long)(e-p) + 1); strncpy(kws->uri, p, e-p); *(kws->uri + (e-p)) = '\0'; @@ -353,7 +354,7 @@ static int ws_server_handshake(kws_t *kws) KS_DECLARE(ks_ssize_t) kws_raw_read(kws_t *kws, void *data, ks_size_t bytes, int block) { - ks_ssize_t r; + int r; int err = 0; kws->x++; @@ -361,7 +362,7 @@ KS_DECLARE(ks_ssize_t) kws_raw_read(kws_t *kws, void *data, ks_size_t bytes, int if (kws->ssl) { do { - r = SSL_read(kws->ssl, data, bytes); + r = SSL_read(kws->ssl, data, (int)bytes); if (r == -1) { err = SSL_get_error(kws->ssl, r); @@ -386,7 +387,7 @@ KS_DECLARE(ks_ssize_t) kws_raw_read(kws_t *kws, void *data, ks_size_t bytes, int do { - r = recv(kws->sock, data, bytes, 0); + r = recv(kws->sock, data, (int)bytes, 0); if (r == -1) { if (!block && ks_errno_is_blocking(ks_errno())) { @@ -420,14 +421,14 @@ KS_DECLARE(ks_ssize_t) kws_raw_read(kws_t *kws, void *data, ks_size_t bytes, int KS_DECLARE(ks_ssize_t) kws_raw_write(kws_t *kws, void *data, ks_size_t bytes) { - ks_ssize_t r; + int r; int sanity = WS_WRITE_SANITY; int ssl_err = 0; ks_size_t wrote = 0; if (kws->ssl) { do { - r = SSL_write(kws->ssl, (void *)((unsigned char *)data + wrote), bytes - wrote); + r = SSL_write(kws->ssl, (void *)((unsigned char *)data + wrote), (int)(bytes - wrote)); if (r > 0) { wrote += r; @@ -460,7 +461,7 @@ KS_DECLARE(ks_ssize_t) kws_raw_write(kws_t *kws, void *data, ks_size_t bytes) } do { - r = send(kws->sock, (void *)((unsigned char *)data + wrote), bytes - wrote, 0); + r = send(kws->sock, (void *)((unsigned char *)data + wrote), (int)(bytes - wrote), 0); if (r > 0) { wrote += r; @@ -516,7 +517,7 @@ static int establish_client_logical_layer(kws_t *kws) kws->ssl = SSL_new(kws->ssl_ctx); assert(kws->ssl); - SSL_set_fd(kws->ssl, kws->sock); + SSL_set_fd(kws->ssl, (int)kws->sock); } do { @@ -593,7 +594,7 @@ static int establish_server_logical_layer(kws_t *kws) kws->ssl = SSL_new(kws->ssl_ctx); assert(kws->ssl); - SSL_set_fd(kws->ssl, kws->sock); + SSL_set_fd(kws->ssl, (int)kws->sock); } do { @@ -707,8 +708,8 @@ KS_DECLARE(ks_status_t) kws_init(kws_t **kwsP, ks_socket_t sock, SSL_CTX *ssl_ct kws->buflen = 1024 * 64; kws->bbuflen = kws->buflen; - kws->buffer = ks_pool_alloc(kws->pool, kws->buflen); - kws->bbuffer = ks_pool_alloc(kws->pool, kws->bbuflen); + kws->buffer = ks_pool_alloc(kws->pool, (unsigned long)kws->buflen); + kws->bbuffer = ks_pool_alloc(kws->pool, (unsigned long)kws->bbuflen); //printf("init %p %ld\n", (void *) kws->bbuffer, kws->bbuflen); //memset(kws->buffer, 0, kws->buflen); //memset(kws->bbuffer, 0, kws->bbuflen); @@ -946,7 +947,7 @@ KS_DECLARE(ks_ssize_t) kws_read_frame(kws_t *kws, kws_opcode_t *oc, uint8_t **da if (kws->plen == 127) { uint64_t *u64; - int more = 0; + ks_ssize_t more = 0; need += 8; @@ -955,7 +956,7 @@ KS_DECLARE(ks_ssize_t) kws_read_frame(kws_t *kws, kws_opcode_t *oc, uint8_t **da //*oc = WSOC_CLOSE; //return kws_close(kws, WS_PROTO_ERR); - more = kws_raw_read(kws, kws->buffer + kws->datalen, need - kws->datalen, WS_BLOCK); + more = kws_raw_read(kws, kws->buffer + kws->datalen, (int)(need - kws->datalen), WS_BLOCK); if (more < 0 || more < need - kws->datalen) { *oc = WSOC_CLOSE; @@ -999,14 +1000,14 @@ KS_DECLARE(ks_ssize_t) kws_read_frame(kws_t *kws, kws_opcode_t *oc, uint8_t **da return kws_close(kws, WS_NONE); } - blen = kws->body - kws->bbuffer; + blen = (int)(kws->body - kws->bbuffer); if (need + blen > (ks_ssize_t)kws->bbuflen) { void *tmp; kws->bbuflen = need + blen + kws->rplen; - if ((tmp = ks_pool_resize(kws->pool, kws->bbuffer, kws->bbuflen))) { + if ((tmp = ks_pool_resize(kws->pool, kws->bbuffer, (unsigned long)kws->bbuflen))) { kws->bbuffer = tmp; } else { abort(); @@ -1146,7 +1147,7 @@ KS_DECLARE(ks_ssize_t) kws_write_frame(kws_t *kws, kws_opcode_t oc, void *data, void *tmp; kws->write_buffer_len = hlen + bytes + 1; - if ((tmp = ks_pool_resize(kws->pool, kws->write_buffer, kws->write_buffer_len))) { + if ((tmp = ks_pool_resize(kws->pool, kws->write_buffer, (unsigned long)kws->write_buffer_len))) { kws->write_buffer = tmp; } else { abort();