artemsen / swayimg

Image viewer for Wayland
MIT License
367 stars 30 forks source link

Fix warnings on 32-bit platforms #159

Closed voidbert closed 3 months ago

voidbert commented 3 months ago

Fixed code that assumed the size of some integer types to be a fixed number of bytes, when it can very between platforms. The warnings fixed were:

I didn't test if these warnings caused any runtime errors, but they can do so, so it's better to have them fixed.

voidbert commented 3 months ago

I hereby attach the warnings that resulted from compiling swayimg on GCC 13.2.0, on a 32-bit glibc system.

warnings.txt ``` ninja: Entering directory `/root/swayimg/build' [1/34] Generating xdg-shell-protocol.c with a custom command [2/34] Generating xdg-shell-protocol.h with a custom command [3/34] Compiling C object swayimg.p/meson-generated_.._xdg-shell-protocol.c.o [4/34] Compiling C object swayimg.p/src_action.c.o [5/34] Compiling C object swayimg.p/src_image.c.o [6/34] Compiling C object swayimg.p/src_cache.c.o [7/34] Compiling C object swayimg.p/src_config.c.o ../src/config.c: In function 'load_config': ../src/config.c:122:65: warning: format '%lu' expects argument of type 'long unsigned int', but argument 4 has type 'size_t' {aka 'unsigned int'} [-Wformat=] 122 | fprintf(stderr, "Invalid section define in %s:%lu\n", path, | ~~^ | | | long unsigned int | %u 123 | line_num); | ~~~~~~~~ | | | size_t {aka unsigned int} ../src/config.c:135:63: warning: format '%lu' expects argument of type 'long unsigned int', but argument 4 has type 'size_t' {aka 'unsigned int'} [-Wformat=] 135 | fprintf(stderr, "Invalid key=value format in %s:%lu\n", path, | ~~^ | | | long unsigned int | %u 136 | line_num); | ~~~~~~~~ | | | size_t {aka unsigned int} ../src/config.c:154:60: warning: format '%lu' expects argument of type 'long unsigned int', but argument 4 has type 'size_t' {aka 'unsigned int'} [-Wformat=] 154 | fprintf(stderr, "Invalid configuration in %s:%lu\n", path, | ~~^ | | | long unsigned int | %u 155 | line_num); | ~~~~~~~~ | | | size_t {aka unsigned int} ../src/config.c: In function 'config_to_color': ../src/config.c:302:58: warning: comparison of integer expressions of different signedness: 'ssize_t' {aka 'int'} and 'unsigned int' [-Wsign-compare] 302 | if (!str_to_num(text, 0, &num, 16) || num < 0 || num > 0xffffffff) { | ^ [8/34] Compiling C object swayimg.p/src_font.c.o [9/34] Compiling C object swayimg.p/src_keybind.c.o [10/34] Compiling C object swayimg.p/src_main.c.o ../src/main.c: In function 'sway_setup': ../src/main.c:170:27: warning: comparison of integer expressions of different signedness: 'ssize_t' {aka 'int'} and 'unsigned int' [-Wsign-compare] 170 | absolute = ui_get_x() != POS_FROM_PARENT && ui_get_y() != POS_FROM_PARENT; | ^~ ../src/main.c:170:60: warning: comparison of integer expressions of different signedness: 'ssize_t' {aka 'int'} and 'unsigned int' [-Wsign-compare] 170 | absolute = ui_get_x() != POS_FROM_PARENT && ui_get_y() != POS_FROM_PARENT; | ^~ [11/34] Compiling C object swayimg.p/src_imagelist.c.o [12/34] Compiling C object swayimg.p/src_info.c.o ../src/info.c: In function 'info_update': ../src/info.c:400:45: warning: format '%lu' expects argument of type 'long unsigned int', but argument 4 has type 'size_t' {aka 'unsigned int'} [-Wformat=] 400 | snprintf(buffer, sizeof(buffer), "%lu of %lu", ctx.frame + 1, | ~~^ ~~~~~~~~~~~~~ | | | | long unsigned int size_t {aka unsigned int} | %u ../src/info.c:400:52: warning: format '%lu' expects argument of type 'long unsigned int', but argument 5 has type 'size_t' {aka 'unsigned int'} [-Wformat=] 400 | snprintf(buffer, sizeof(buffer), "%lu of %lu", ctx.frame + 1, | ~~^ | | | long unsigned int | %u 401 | ctx.frame_total); | ~~~~~~~~~~~~~~~ | | | size_t {aka unsigned int} ../src/info.c:407:45: warning: format '%lu' expects argument of type 'long unsigned int', but argument 4 has type 'size_t' {aka 'unsigned int'} [-Wformat=] 407 | snprintf(buffer, sizeof(buffer), "%lu of %lu", ctx.index + 1, | ~~^ ~~~~~~~~~~~~~ | | | | long unsigned int size_t {aka unsigned int} | %u ../src/info.c:407:52: warning: format '%lu' expects argument of type 'long unsigned int', but argument 5 has type 'size_t' {aka 'unsigned int'} [-Wformat=] 407 | snprintf(buffer, sizeof(buffer), "%lu of %lu", ctx.index + 1, | ~~^ | | | long unsigned int | %u 408 | image_list_size()); | ~~~~~~~~~~~~~~~~~ | | | size_t {aka unsigned int} ../src/info.c:416:49: warning: format '%ld' expects argument of type 'long int', but argument 4 has type 'size_t' {aka 'unsigned int'} [-Wformat=] 416 | snprintf(buffer, sizeof(buffer), "%ld%%", ctx.scale); | ~~^ ~~~~~~~~~ | | | | long int size_t {aka unsigned int} | %d ../src/info.c:426:49: warning: format '%lu' expects argument of type 'long unsigned int', but argument 4 has type 'size_t' {aka 'unsigned int'} [-Wformat=] 426 | snprintf(buffer, sizeof(buffer), "%lux%lu", ctx.width, ctx.height); | ~~^ ~~~~~~~~~ | | | | | size_t {aka unsigned int} | long unsigned int | %u ../src/info.c:426:53: warning: format '%lu' expects argument of type 'long unsigned int', but argument 5 has type 'size_t' {aka 'unsigned int'} [-Wformat=] 426 | snprintf(buffer, sizeof(buffer), "%lux%lu", ctx.width, ctx.height); | ~~^ ~~~~~~~~~~ | | | | long unsigned int size_t {aka unsigned int} | %u [13/34] Compiling C object swayimg.p/src_text.c.o [14/34] Compiling C object swayimg.p/src_str.c.o [15/34] Compiling C object swayimg.p/src_loader.c.o [16/34] Compiling C object swayimg.p/src_sway.c.o [17/34] Compiling C object swayimg.p/src_formats_pnm.c.o [18/34] Compiling C object swayimg.p/src_ui.c.o ../src/ui.c: In function 'create_buffer': ../src/ui.c:161:25: warning: left shift count >= width of type [-Wshift-count-overflow] 161 | (ts.tv_sec << 32) | ts.tv_nsec); | ^~ ../src/ui.c: In function 'ui_create': ../src/ui.c:701:47: warning: left shift count >= width of type [-Wshift-count-overflow] 701 | const uint64_t timestamp = (ts.tv_sec << 32) | ts.tv_nsec; | ^~ In file included from ../src/ui.c:7: ./buildcfg.h:8:18: warning: format '%lx' expects argument of type 'long unsigned int', but argument 4 has type 'uint64_t' {aka 'long long unsigned int'} [-Wformat=] 8 | #define APP_NAME "swayimg" | ^~~~~~~~~ ../src/ui.c:702:42: note: in expansion of macro 'APP_NAME' 702 | snprintf(app_id, sizeof(app_id), APP_NAME "_%lx", timestamp); | ^~~~~~~~ ../src/ui.c:702:55: note: format string is defined here 702 | snprintf(app_id, sizeof(app_id), APP_NAME "_%lx", timestamp); | ~~^ | | | long unsigned int | %llx [19/34] Compiling C object swayimg.p/src_formats_bmp.c.o [20/34] Compiling C object swayimg.p/src_formats_tga.c.o [21/34] Compiling C object swayimg.p/src_pixmap.c.o [22/34] Compiling C object swayimg.p/src_exif.c.o [23/34] Compiling C object swayimg.p/src_viewer.c.o [24/34] Compiling C object swayimg.p/src_formats_avif.c.o [25/34] Compiling C object swayimg.p/src_formats_jpeg.c.o [26/34] Compiling C object swayimg.p/src_formats_heif.c.o [27/34] Compiling C object swayimg.p/src_formats_jxl.c.o [28/34] Compiling C object swayimg.p/src_formats_gif.c.o [29/34] Compiling C object swayimg.p/src_formats_png.c.o [30/34] Compiling C object swayimg.p/src_formats_webp.c.o [31/34] Compiling C object swayimg.p/src_formats_exr.c.o [32/34] Compiling C object swayimg.p/src_formats_tiff.c.o [33/34] Compiling C object swayimg.p/src_formats_svg.c.o [34/34] Linking target swayimg INFO: autodetecting backend as ninja INFO: calculating backend command to run: /bin/ninja -C /root/swayimg/build ```
artemsen commented 3 months ago

Sounds reasonable, thank you!