From cd454dff3bc20d5139c408f33bb51f0d336e8c64 Mon Sep 17 00:00:00 2001
From: Mathis Rosenhauer <rosenhauer@dkrz.de>
Date: Thu, 21 Mar 2024 08:29:19 +0100
Subject: [PATCH] Fix compiler warnings (#31)

---
 .github/workflows/cmake-multi-platform.yml |  3 +-
 CHANGELOG.md                               |  6 ++++
 CMakeLists.txt                             |  3 --
 cmake/config.h.in                          |  2 +-
 cmake/libaec-config.cmake.in               | 12 ++++++++
 include/libaec.h.in                        |  8 +++++-
 src/CMakeLists.txt                         | 20 ++++++++------
 src/Makefile.am                            |  5 ++--
 src/decode.c                               |  4 +--
 src/encode.c                               |  3 +-
 src/sz_compat.c                            |  4 +--
 src/vector.h                               |  2 +-
 tests/check_rsi_block_access.c             | 32 ++--------------------
 tests/check_seeking.c                      |  2 +-
 14 files changed, 53 insertions(+), 53 deletions(-)

diff --git a/.github/workflows/cmake-multi-platform.yml b/.github/workflows/cmake-multi-platform.yml
index 8cdea91..5f79104 100644
--- a/.github/workflows/cmake-multi-platform.yml
+++ b/.github/workflows/cmake-multi-platform.yml
@@ -2,7 +2,7 @@
 # See: https://github.com/actions/starter-workflows/blob/main/ci/cmake-single-platform.yml
 name: CMake on multiple platforms
 
-on: [push, pull_request, workflow_dispatch]
+on: [push, workflow_dispatch]
 
 jobs:
   build:
@@ -58,6 +58,7 @@ jobs:
         -DCMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }}
         -DCMAKE_C_COMPILER=${{ matrix.c_compiler }}
         -DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
+        -DCMAKE_C_FLAGS=${{ matrix.c_compiler == 'gcc' && '-Wall -Wundef -Werror' || '' }}
         -S ${{ github.workspace }}
 
     - name: Build
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bb84b2b..a2412d7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,6 +1,12 @@
 # libaec Changelog
 All notable changes to libaec will be documented in this file.
 
+## [1.1.3] - 2024-03-21
+
+### Fixed
+- Compiler warnings
+
+
 ## [1.1.2] - 2023-10-04
 
 ### Fixed
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 44caec6..75a64f8 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,9 +1,6 @@
 cmake_minimum_required(VERSION 3.13...3.19)
 project(libaec LANGUAGES C VERSION 1.1.2)
 
-# Automatically export symbols for Windows DLLs
-set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
-
 set(CMAKE_C_STANDARD 99)
 set(CMAKE_C_VISIBILITY_PRESET hidden)
 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
diff --git a/cmake/config.h.in b/cmake/config.h.in
index 9687144..25f30ee 100644
--- a/cmake/config.h.in
+++ b/cmake/config.h.in
@@ -1,6 +1,6 @@
 #cmakedefine WORDS_BIGENDIAN
 #cmakedefine01 HAVE_DECL___BUILTIN_CLZLL
-#cmakedefine01 HAVE_BSR64
+#cmakedefine HAVE_BSR64
 #cmakedefine HAVE_SNPRINTF
 #cmakedefine HAVE__SNPRINTF
 #cmakedefine HAVE__SNPRINTF_S
diff --git a/cmake/libaec-config.cmake.in b/cmake/libaec-config.cmake.in
index 5054da8..6f6c9e9 100644
--- a/cmake/libaec-config.cmake.in
+++ b/cmake/libaec-config.cmake.in
@@ -59,6 +59,12 @@ if (libaec_FOUND)
     add_library(libaec::aec STATIC IMPORTED)
   else ()
     add_library(libaec::aec SHARED IMPORTED)
+    target_compile_definitions(libaec::aec INTERFACE LIBAEC_SHARED)
+    if (MSVC)
+      set_target_properties(libaec::aec PROPERTIES
+        IMPORTED_IMPLIB "${libaec_LIBRARY}"
+      )
+    endif ()
   endif ()
   set_target_properties(libaec::aec PROPERTIES
     IMPORTED_LOCATION "${libaec_LIBRARY}"
@@ -70,6 +76,12 @@ if (libaec_FOUND)
     add_library(libaec::sz STATIC IMPORTED)
   else ()
     add_library(libaec::sz SHARED IMPORTED)
+    target_compile_definitions(libaec::sz INTERFACE LIBAEC_SHARED)
+    if (MSVC)
+      set_target_properties(libaec::sz PROPERTIES
+        IMPORTED_IMPLIB "${SZIP_LIBRARY}"
+      )
+    endif ()
   endif ()
   set_target_properties(libaec::sz PROPERTIES
     IMPORTED_LOCATION "${SZIP_LIBRARY}"
diff --git a/include/libaec.h.in b/include/libaec.h.in
index 26d2fe8..e135432 100644
--- a/include/libaec.h.in
+++ b/include/libaec.h.in
@@ -49,8 +49,14 @@
 extern "C"{
 #endif
 
-#if BUILDING_LIBAEC && HAVE_VISIBILITY
+#if defined LIBAEC_BUILD && HAVE_VISIBILITY
 #  define LIBAEC_DLL_EXPORTED __attribute__((__visibility__("default")))
+#elif (defined _WIN32 && !defined __CYGWIN__) && defined LIBAEC_SHARED
+#  if defined LIBAEC_BUILD
+#    define LIBAEC_DLL_EXPORTED __declspec(dllexport)
+#  else
+#    define LIBAEC_DLL_EXPORTED __declspec(dllimport)
+#  endif
 #else
 #  define LIBAEC_DLL_EXPORTED
 #endif
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 7abf455..7801d82 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -31,6 +31,8 @@ set_target_properties(aec_shared
 add_library(sz OBJECT sz_compat.c)
 target_link_libraries(sz PUBLIC aec)
 
+set(libaec_COMPILE_DEFINITIONS "LIBAEC_BUILD;LIBAEC_SHARED")
+
 # Create both static and shared szip library.
 add_library(sz_static STATIC "$<TARGET_OBJECTS:sz>" "$<TARGET_OBJECTS:aec>")
 set_target_properties(sz_static
@@ -51,16 +53,14 @@ set_target_properties(sz_shared
 add_executable(graec graec.c)
 target_link_libraries(graec aec)
 
+# Handle visibility of symbols. Compatible with gnulib's gl_VISIBILITY
+include(CheckCCompilerFlag)
+check_c_compiler_flag(-fvisibility=hidden COMPILER_HAS_HIDDEN_VISIBILITY)
+set(libaec_COMPILE_DEFINITIONS
+  "${libaec_COMPILE_DEFINITIONS};HAVE_VISIBILITY=$<BOOL:${COMPILER_HAS_HIDDEN_VISIBILITY}>")
+
 include(GNUInstallDirs)
 if(UNIX)
-  # Handle visibility of symbols. Compatible with gnulib's gl_VISIBILITY
-  include(CheckCCompilerFlag)
-  check_c_compiler_flag(-fvisibility=hidden COMPILER_HAS_HIDDEN_VISIBILITY)
-  set_target_properties(aec sz
-    PROPERTIES
-    COMPILE_DEFINITIONS
-    "HAVE_VISIBILITY=$<BOOL:${COMPILER_HAS_HIDDEN_VISIBILITY}>;BUILDING_LIBAEC")
-
   # The shell scripts for benchmarking are supported on unix only
   add_executable(utime EXCLUDE_FROM_ALL utime.c)
   target_include_directories(utime PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/..")
@@ -71,4 +71,8 @@ if(UNIX)
     DEPENDS graec utime)
 endif()
 
+set_target_properties(aec sz
+  PROPERTIES
+  COMPILE_DEFINITIONS "${libaec_COMPILE_DEFINITIONS}")
+
 install(TARGETS aec_static aec_shared sz_static sz_shared)
diff --git a/src/Makefile.am b/src/Makefile.am
index cb98d06..89eb635 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,14 +1,15 @@
 AUTOMAKE_OPTIONS = no-installman
 AM_CFLAGS = $(CFLAG_VISIBILITY)
-AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_builddir)/include \
--DBUILDING_LIBAEC
+AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_builddir)/include
 lib_LTLIBRARIES = libaec.la libsz.la
 libaec_la_SOURCES = encode.c encode_accessors.c decode.c vector.c\
 encode.h encode_accessors.h decode.h vector.h
+libaec_la_CPPFLAGS = $(AM_CPPFLAGS) -DLIBAEC_BUILD
 libaec_la_LDFLAGS = -version-info 1:2:1 -no-undefined
 
 libsz_la_SOURCES = sz_compat.c
 libsz_la_LIBADD = libaec.la
+libsz_la_CPPFLAGS = $(AM_CPPFLAGS) -DLIBAEC_BUILD
 libsz_la_LDFLAGS = -version-info 2:1:0 -no-undefined
 
 include_HEADERS = $(top_builddir)/include/libaec.h $(top_srcdir)/include/szlib.h
diff --git a/src/decode.c b/src/decode.c
index b742e2c..0da08d3 100644
--- a/src/decode.c
+++ b/src/decode.c
@@ -43,7 +43,7 @@
 #include <stdlib.h>
 #include <string.h>
 
-#if HAVE_BSR64
+#ifdef HAVE_BSR64
 #include <intrin.h>
 #endif
 
@@ -295,7 +295,7 @@ static inline uint32_t direct_get_fs(struct aec_stream *strm)
 #endif
 #if HAVE_DECL___BUILTIN_CLZLL || __has_builtin(__builtin_clzll)
         int i = 63 - __builtin_clzll(state->acc);
-#elif HAVE_BSR64
+#elif defined HAVE_BSR64
         unsigned long i;
         _BitScanReverse64(&i, state->acc);
 #else
diff --git a/src/encode.c b/src/encode.c
index 34dafa4..874cff5 100644
--- a/src/encode.c
+++ b/src/encode.c
@@ -337,7 +337,7 @@ static uint32_t assess_splitting_option(struct aec_stream *strm)
     struct internal_state *state = strm->state;
 
     /* Block size of current block */
-    int this_bs = strm->block_size - state->ref;
+    uint64_t this_bs = strm->block_size - state->ref;
 
     /* CDS length minimum so far */
     uint64_t len_min = UINT64_MAX;
@@ -890,7 +890,6 @@ int aec_encode_init(struct aec_stream *strm)
     *state->cds = 0;
     state->bits = 8;
     state->mode = m_get_block;
-    struct vector_t *offsets = NULL;
     state->ready_to_capture_rsi = 0;
     return AEC_OK;
 }
diff --git a/src/sz_compat.c b/src/sz_compat.c
index a9a5398..1b1c6f7 100644
--- a/src/sz_compat.c
+++ b/src/sz_compat.c
@@ -71,7 +71,7 @@ static int bits_to_bytes(int bit_length)
 }
 
 static void interleave_buffer(void *dest, const void *src,
-                              size_t n, int wordsize)
+                              size_t n, size_t wordsize)
 {
     const unsigned char *src8 = (unsigned char *)src;
     unsigned char *dest8 = (unsigned char *)dest;
@@ -82,7 +82,7 @@ static void interleave_buffer(void *dest, const void *src,
 }
 
 static void deinterleave_buffer(void *dest, const void *src,
-                                size_t n, int wordsize)
+                                size_t n, size_t wordsize)
 {
     const unsigned char *src8 = (unsigned char *)src;
     unsigned char *dest8 = (unsigned char *)dest;
diff --git a/src/vector.h b/src/vector.h
index 747661e..6a702b4 100644
--- a/src/vector.h
+++ b/src/vector.h
@@ -1,6 +1,6 @@
 #pragma once
 
-#include <libaec.h>
+#include "config.h"
 #include <stdlib.h>
 
 struct vector_t {
diff --git a/tests/check_rsi_block_access.c b/tests/check_rsi_block_access.c
index 1260034..d1b135a 100644
--- a/tests/check_rsi_block_access.c
+++ b/tests/check_rsi_block_access.c
@@ -112,22 +112,6 @@ static void data_generator_incr(struct aec_context *ctx)
     }
 }
 
-static void ctx_init(struct aec_context *ctx)
-{
-    ctx->nvalues = 0;
-    ctx->flags = 0;
-    ctx->rsi = 0;
-    ctx->block_size = 0;
-    ctx->bits_per_sample = 0;
-    ctx->obuf = NULL;
-    ctx->ebuf = NULL;
-    ctx->dbuf = NULL;
-    ctx->obuf_len = 0;
-    ctx->ebuf_len = 0;
-    ctx->dbuf_len = 0;
-    ctx->ebuf_total = 0;
-}
-
 #define PREPARE_ENCODE(strm_e, ctx, flags) \
 { \
     (strm_e)->flags = flags; \
@@ -239,7 +223,6 @@ static int test_rsi_at(struct aec_context *ctx)
 {
     int status = AEC_OK;
     int flags = ctx->flags;
-    unsigned short *obuf = (unsigned short*) ctx->obuf;
 
     struct aec_stream strm_encode;
     PREPARE_ENCODE(&strm_encode, ctx, flags);
@@ -256,7 +239,7 @@ static int test_rsi_at(struct aec_context *ctx)
         exit(1);
     }
 
-    for (int i = 0; i < offsets_count; ++i) {
+    for (size_t i = 0; i < offsets_count; ++i) {
         struct aec_stream strm_at;
         strm_at.flags = flags;
         strm_at.rsi = ctx->rsi;
@@ -270,8 +253,8 @@ static int test_rsi_at(struct aec_context *ctx)
         if ((status = aec_rsi_at(&strm_at, offsets, offsets_count, i)) != AEC_OK) {
             return status;
         }
-        for (int j = 0; j < strm_at.total_out; j++) {
-            if (j == ctx->rsi * ctx->block_size * ctx->bytes_per_sample + j > ctx->obuf_len) {
+        for (size_t j = 0; j < strm_at.total_out; j++) {
+            if (j == (ctx->rsi * ctx->block_size * ctx->bytes_per_sample + j > ctx->obuf_len)) {
                 break;
             }
             if (rsi_buf[j] != ctx->obuf[i * ctx->block_size * ctx->rsi * ctx->bytes_per_sample + j]) {
@@ -297,11 +280,6 @@ int test_read(struct aec_context *ctx)
     size_t *offsets = NULL;
     size_t offsets_size = 0;
     PREPARE_DECODE_WITH_OFFSETS(&strm_decode, ctx, flags, offsets, &offsets_size);
-
-    size_t rsi_len = ctx->rsi * ctx->block_size * ctx->bytes_per_sample;
-    unsigned rsi_n = ctx->obuf_len / (ctx->rsi * ctx->block_size); // Number of full rsi blocks
-    unsigned rsi_r = ctx->obuf_len % (ctx->rsi * ctx->block_size); // Remainder
-
     // Edge case: Imposible to get wanted number of slices
     size_t wanted_num_slices = 3;
     if (wanted_num_slices > ctx->obuf_len) {
@@ -340,7 +318,6 @@ int test_read(struct aec_context *ctx)
 
     if ((status = aec_decode_init(&strm_read)) != AEC_OK)
         return status;
-    struct internal_state *state = strm_read.state;
 
     // Test 1: Stream data
     for (size_t i = 0; i < num_slices; ++i) {
@@ -360,7 +337,6 @@ int test_read(struct aec_context *ctx)
 
     // Test 2: Read slices
     for (size_t i = 0; i < num_slices; ++i) {
-        struct internal_state *state = strm_read.state;
         size_t buf_size = slice_sizes[i];;
         unsigned char *buf = malloc(buf_size);
         if (buf == NULL) {
@@ -413,8 +389,6 @@ int test_offsets(struct aec_context *ctx)
         return 102;
     }
 
-    size_t size = decode_offsets_size > 10 ? 10 : decode_offsets_size;
-
     for (size_t i = 0; i < encode_offsets_size; ++i) {
         if (encode_offsets_ptr[i] != decode_offsets_ptr[i]) {
             fprintf(stderr, "Error: encode_offsets_ptr[%zu] = %zu, decode_offsets_ptr[%zu] = %zu\n", i, encode_offsets_ptr[i], i, decode_offsets_ptr[i]);
diff --git a/tests/check_seeking.c b/tests/check_seeking.c
index 1fca2f9..6c12aa7 100644
--- a/tests/check_seeking.c
+++ b/tests/check_seeking.c
@@ -14,7 +14,7 @@ void shift_cdata(struct test_state *state, unsigned char *cbuf_unshifted,
     unsigned char *dst = state->cbuf + byte_offset;
 
     memset(state->cbuf, 0, state->buf_len);
-    for (int i = 0; i < strm->avail_in; i++) {
+    for (size_t i = 0; i < strm->avail_in; i++) {
         dst[i] |= cbuf_unshifted[i] >> bit_offset;
         dst[i + 1] |= cbuf_unshifted[i] << (8 - bit_offset);
     }
-- 
GitLab