From 4448b6494e0f6bece719160e652fb872be32eb21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Valverde?= Date: Mon, 20 Dec 2021 02:33:15 +0000 Subject: [PATCH] Add a ws_posix_compat.h header Currently used to define ssize_t on platforms that lack it. Fix some Windows build errors caused by moving the definition into a separate header. Fix some narrowing warnings on Windows x64 from changing the definition of ssize_t from long int to int64_t. The casts in dumpcap are ugly but necessary. The whole code needs to be rewritten for portability, or the warnings disabled. --- CMakeLists.txt | 1 + cmakeconfig.h.in | 4 ---- doc/plugins.example/CMakeLists.txt | 10 +++++++-- dumpcap.c | 14 ++++++++++++- include/wireshark.h | 1 + include/ws_posix_compat.h | 33 ++++++++++++++++++++++++++++++ plugins/codecs/sbc/sbc.c | 2 +- sync_pipe.h | 1 + wiretap/file_wrappers.c | 2 +- wsutil/filesystem.c | 2 +- 10 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 include/ws_posix_compat.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 38fb0fa083..4df126792e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3432,6 +3432,7 @@ set(SHARK_PUBLIC_HEADERS include/ws_compiler_tests.h include/ws_diag_control.h include/ws_log_defs.h + include/ws_posix_compat.h include/ws_symbol_export.h include/wireshark.h ${CMAKE_BINARY_DIR}/ws_version.h diff --git a/cmakeconfig.h.in b/cmakeconfig.h.in index 8ed1662cf3..c8a7d414ea 100644 --- a/cmakeconfig.h.in +++ b/cmakeconfig.h.in @@ -306,10 +306,6 @@ /* Define to 1 if the 'ssize_t' type exists. */ #cmakedefine HAVE_SSIZE_T 1 -#ifndef HAVE_SSIZE_T -# define ssize_t long int -#endif - #if defined(_MSC_VER) # define strncasecmp strnicmp # define popen _popen diff --git a/doc/plugins.example/CMakeLists.txt b/doc/plugins.example/CMakeLists.txt index 41242dbe52..223abd6662 100644 --- a/doc/plugins.example/CMakeLists.txt +++ b/doc/plugins.example/CMakeLists.txt @@ -7,7 +7,7 @@ # SPDX-License-Identifier: GPL-2.0-or-later # -cmake_minimum_required(VERSION 3.1) +cmake_minimum_required(VERSION 3.12) cmake_policy(SET CMP0048 NEW) project(Hello VERSION 0.0.1 DESCRIPTION "Wireshark Hello Plugin" LANGUAGES C) @@ -18,12 +18,18 @@ if(NOT Wireshark_PLUGINS_ENABLED) message(WARNING "Wireshark was compiled without support for plugins") endif() +include(CheckTypeSize) +check_type_size("ssize_t" SSIZE_T) + set(CMAKE_C_VISIBILITY_PRESET hidden) if (CMAKE_COMPILER_IS_GNUCC) set(CMAKE_C_FLAGS "-Wall -Wextra ${CMAKE_C_FLAGS}") endif() -add_definitions(-DVERSION=\"${PROJECT_VERSION}\") +add_compile_definitions( + VERSION=\"${PROJECT_VERSION}\" + $<$:HAVE_SSIZE_T> +) add_library(hello MODULE hello.c) set_target_properties(hello PROPERTIES PREFIX "" DEFINE_SYMBOL "") diff --git a/dumpcap.c b/dumpcap.c index fe1b133d80..3492283180 100644 --- a/dumpcap.c +++ b/dumpcap.c @@ -1260,7 +1260,7 @@ static void *cap_thread_read(void *arg) break; } } else { - bytes_read += b; + bytes_read += (DWORD)b; } } #ifdef _WIN32 @@ -1485,7 +1485,11 @@ cap_pipe_read_data_bytes(capture_src *pcap_src, char *errmsg, size_t errmsgl) } return -1; } +#ifdef _WIN32 + bytes_read += (DWORD)b; +#else bytes_read += b; +#endif } } pcap_src->cap_pipe_bytes_read += bytes_read; @@ -2317,7 +2321,11 @@ pcap_pipe_dispatch(loop_data *ld, capture_src *pcap_src, char *errmsg, size_t er result = PD_PIPE_ERR; break; } +#ifdef _WIN32 + pcap_src->cap_pipe_bytes_read += (DWORD)b; +#else pcap_src->cap_pipe_bytes_read += b; +#endif } #ifdef _WIN32 else { @@ -2374,7 +2382,11 @@ pcap_pipe_dispatch(loop_data *ld, capture_src *pcap_src, char *errmsg, size_t er result = PD_PIPE_ERR; break; } +#ifdef _WIN32 + pcap_src->cap_pipe_bytes_read += (DWORD)b; +#else pcap_src->cap_pipe_bytes_read += b; +#endif } #ifdef _WIN32 else { diff --git a/include/wireshark.h b/include/wireshark.h index 26d7b49f05..29a68d050e 100644 --- a/include/wireshark.h +++ b/include/wireshark.h @@ -43,6 +43,7 @@ #include #include #include +#include #include #include diff --git a/include/ws_posix_compat.h b/include/ws_posix_compat.h new file mode 100644 index 0000000000..922e0a2b9a --- /dev/null +++ b/include/ws_posix_compat.h @@ -0,0 +1,33 @@ +/* ws_posix_compat.h + * Definitions for POSIX compatibility. + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 1998 Gerald Combs + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef __POSIX_COMPAT_H__ +#define __POSIX_COMPAT_H__ + +#include +#include + +#if !defined(SSIZE_MAX) && !defined(HAVE_SSIZE_T) +#if defined(_WIN64) + +typedef int64_t ssize_t; + +#define SSIZE_MAX INT64_MAX + +#else /* !_WIN64 */ + +typedef signed long int ssize_t; + +#define SSIZE_MAX LONG_MAX + +#endif /* _WIN64 */ +#endif /* !SSIZE_MAX && !HAVE_SSIZE_T */ + +#endif /* __POSIX_COMPAT_H__ */ diff --git a/plugins/codecs/sbc/sbc.c b/plugins/codecs/sbc/sbc.c index 630d8d676f..ffe1f6803a 100644 --- a/plugins/codecs/sbc/sbc.c +++ b/plugins/codecs/sbc/sbc.c @@ -12,7 +12,7 @@ #include "config.h" -#include +#include #include #include "wsutil/codecs.h" diff --git a/sync_pipe.h b/sync_pipe.h index 0a08b72da9..ffc473711d 100644 --- a/sync_pipe.h +++ b/sync_pipe.h @@ -18,6 +18,7 @@ #ifndef __SYNC_PIPE_H__ #define __SYNC_PIPE_H__ +#include /* * Maximum length of sync pipe message data. Must be < 2^24, as the diff --git a/wiretap/file_wrappers.c b/wiretap/file_wrappers.c index 337010d625..a513389648 100644 --- a/wiretap/file_wrappers.c +++ b/wiretap/file_wrappers.c @@ -275,7 +275,7 @@ buf_read(FILE_T state, struct wtap_reader_buf *buf) if (ret == 0) state->eof = TRUE; state->raw_pos += ret; - buf->avail += ret; + buf->avail += (guint)ret; return 0; } diff --git a/wsutil/filesystem.c b/wsutil/filesystem.c index e0bb8e4df9..01b36c4f9b 100644 --- a/wsutil/filesystem.c +++ b/wsutil/filesystem.c @@ -2265,7 +2265,7 @@ gboolean copy_file_binary_mode(const char *from_filename, const char *to_filename) { int from_fd, to_fd, err; - ssize_t nread, nwritten; + int nread, nwritten; guint8 *pd = NULL; /* Copy the raw bytes of the file. */