From a0fbfe1c6d6f42b4c2704c882beda1c4cafe5016 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 24 Oct 2017 11:27:44 +0200 Subject: [SERVER] Fix types or add explicit casts everywhere we might have type conversion problems --- src/shared/fdsignal.c | 1 + src/shared/log.c | 20 ++++++++++---------- src/shared/protocol.h | 4 ++-- src/shared/sockhelper.c | 19 +++++++++++-------- src/shared/sockhelper.h | 4 ++-- 5 files changed, 26 insertions(+), 22 deletions(-) (limited to 'src/shared') diff --git a/src/shared/fdsignal.c b/src/shared/fdsignal.c index 5e81284..5e5cf7f 100644 --- a/src/shared/fdsignal.c +++ b/src/shared/fdsignal.c @@ -7,6 +7,7 @@ //#warning "Using pointer-packing pipe based signalling" #include "fdsignal.inc/pipe64.c" #else +_Static_assert( sizeof(int) != 4 || sizeof(void*) != 8, "Something's goofy, fix preprocessor check above!" ); //#warning "Using fallback pipe based signalling" #include "fdsignal.inc/pipe_malloc.c" #endif diff --git a/src/shared/log.c b/src/shared/log.c index afafc6f..0b385c5 100644 --- a/src/shared/log.c +++ b/src/shared/log.c @@ -89,26 +89,26 @@ void logadd(const logmask_t mask, const char *fmt, ...) ret = vsnprintf( buffer + offset, LINE_LEN - offset, fmt, ap ); va_end( ap ); if ( ret < 0 ) return; - ret += offset; - if ( ret + 1 >= LINE_LEN ) { + offset += ret; + if ( offset + 1 >= LINE_LEN ) { buffer[LINE_LEN-2] = '\0'; - ret = LINE_LEN - 2; + offset = LINE_LEN - 2; } - if ( ret > 0 && buffer[ret-1] != '\n' ) { - buffer[ret++] = '\n'; - buffer[ret] = '\0'; + if ( buffer[offset-1] != '\n' ) { + buffer[offset++] = '\n'; + buffer[offset] = '\0'; } if ( maskFile & mask ) { pthread_mutex_lock( &logLock ); if ( logFd >= 0 ) { - int done = 0; - while (done < ret ) { - const int wr = write( logFd, buffer + done, ret - done ); + size_t done = 0; + while (done < offset ) { + const ssize_t wr = write( logFd, buffer + done, ret - done ); if ( wr < 0 ) { printf( "Logging to file failed! (errno=%d)\n", errno ); break; } - done += wr; + done += (size_t)wr; } } pthread_mutex_unlock( &logLock ); diff --git a/src/shared/protocol.h b/src/shared/protocol.h index c3ccbc1..561e5a1 100644 --- a/src/shared/protocol.h +++ b/src/shared/protocol.h @@ -26,7 +26,7 @@ static inline int dnbd3_read_reply(int sock, dnbd3_reply_t *reply, bool wait) { - int ret = recv( sock, reply, sizeof(*reply), (wait ? MSG_WAITALL : MSG_DONTWAIT) | MSG_NOSIGNAL ); + ssize_t ret = recv( sock, reply, sizeof(*reply), (wait ? MSG_WAITALL : MSG_DONTWAIT) | MSG_NOSIGNAL ); if ( ret == 0 ) return REPLY_CLOSED; if ( ret < 0 ) { if ( errno == EAGAIN || errno == EWOULDBLOCK ) return REPLY_AGAIN; @@ -62,7 +62,7 @@ static inline bool dnbd3_select_image(int sock, const char *name, uint16_t rid, const ssize_t len = serializer_get_written_length( &serialized ); request.magic = dnbd3_packet_magic; request.cmd = CMD_SELECT_IMAGE; - request.size = len; + request.size = (uint32_t)len; #ifdef _DEBUG request.handle = 0; request.offset = 0; diff --git a/src/shared/sockhelper.c b/src/shared/sockhelper.c index e511f0f..118fbec 100644 --- a/src/shared/sockhelper.c +++ b/src/shared/sockhelper.c @@ -160,37 +160,39 @@ void sock_destroyPollList(poll_list_t *list) free( list ); } -int sock_printHost(const dnbd3_host_t * const host, char * const buffer, const int len) +size_t sock_printHost(const dnbd3_host_t * const host, char * const buffer, const size_t len) { // Worst case: Port 5 chars, ':' to separate ip and port 1 char, terminating null 1 char = 7, [] for IPv6 if ( len < 10 ) return 0; char *output = buffer; if ( host->type == AF_INET6 ) { *output++ = '['; - inet_ntop( AF_INET6, host->addr, output, len - 10 ); + inet_ntop( AF_INET6, host->addr, output, (socklen_t)( len - 10 ) ); output += strlen( output ); *output++ = ']'; } else if ( host->type == AF_INET ) { - inet_ntop( AF_INET, host->addr, output, len - 8 ); + inet_ntop( AF_INET, host->addr, output, (socklen_t)( len - 8 ) ); output += strlen( output ); } else { int ret = snprintf( output, len, "", (int)host->type ); - return MIN( ret, len-1 ); + if ( ret <= 0 ) return 0; + return MIN( (size_t)ret, len-1 ); } *output = '\0'; if ( host->port != 0 ) { // There are still at least 7 bytes left in the buffer, port is at most 5 bytes + ':' + '\0' = 7 int ret = snprintf( output, 7, ":%d", (int)ntohs( host->port ) ); + if ( ret < 0 ) ret = 0; output += MIN( ret, 6 ); } return output - buffer; } -int sock_printable(struct sockaddr *addr, socklen_t addrLen, char *output, int len) +size_t sock_printable(const struct sockaddr * const addr, const socklen_t addrLen, char *output, const size_t len) { char host[100], port[10]; int outlen = 0; - int ret = getnameinfo( addr, addrLen, host, 100, port, 10, NI_NUMERICHOST | NI_NUMERICSERV ); + int ret = getnameinfo( addr, addrLen, host, sizeof(host), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV ); if ( ret == 0 ) { if ( addr->sa_family == AF_INET ) { outlen = snprintf( output, len, "%s:%s", host, port ); @@ -198,7 +200,8 @@ int sock_printable(struct sockaddr *addr, socklen_t addrLen, char *output, int l outlen = snprintf( output, len, "[%s]:%s", host, port ); } } - return MIN( outlen, len-1 ); + if ( outlen <= 0 ) return 0; + return MIN( (size_t)outlen, len-1 ); } bool sock_listen(poll_list_t* list, char* bind_addr, uint16_t port) @@ -289,7 +292,7 @@ bool sock_append(poll_list_t *list, const int sock, bool wantRead, bool wantWrit { if ( sock == -1 || list->count >= MAXLISTEN ) return false; list->entry[list->count++].fd = sock; - list->entry[list->count++].events = ( wantRead ? POLLIN : 0 ) | ( wantWrite ? POLLOUT : 0 ) | POLLRDHUP; + list->entry[list->count++].events = (short)( ( wantRead ? POLLIN : 0 ) | ( wantWrite ? POLLOUT : 0 ) | POLLRDHUP ); list->count++; return true; } diff --git a/src/shared/sockhelper.h b/src/shared/sockhelper.h index 62603c5..abfcb9c 100644 --- a/src/shared/sockhelper.h +++ b/src/shared/sockhelper.h @@ -31,9 +31,9 @@ int sock_resolveToDnbd3Host(const char * const address, dnbd3_host_t * const des void sock_setTimeout(const int sockfd, const int milliseconds); -int sock_printHost(const dnbd3_host_t * const host, char *output, const int len); +size_t sock_printHost(const dnbd3_host_t * const host, char *output, const size_t len); -int sock_printable(struct sockaddr *addr, socklen_t addrLen, char *output, int len); +size_t sock_printable(const struct sockaddr * const addr, const socklen_t addrLen, char *output, const size_t len); /** * Create new poll list. -- cgit v1.2.3-55-g7522