Skip to content

Fix format-truncation and strncpy compiler warnings#60

Merged
folkertvanheusden merged 1 commit into
folkertvanheusden:masterfrom
mikelolasagasti:strncpy-warnings
Jan 2, 2026
Merged

Fix format-truncation and strncpy compiler warnings#60
folkertvanheusden merged 1 commit into
folkertvanheusden:masterfrom
mikelolasagasti:strncpy-warnings

Conversation

@mikelolasagasti

Copy link
Copy Markdown
Contributor

Increased buffer sizes in utils.c and ui.c to accommodate the full range of integer values (preventing possible truncation).

Replaced strncpy with snprintf in ui.c to ensure destination buffers are always null-terminated and free of garbage data.

Fixes the following warning:

/builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/utils.c: In function ‘amount_to_str’: /builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/utils.c:371:52: warning: ‘__snprintf_chk’ output may be truncated before the last format character [-Wformat-truncation=]
  371 |                 snprintf(out, AMOUNT_STR_LEN, "%dKB", (int)((amount + M_KB - 1) / M_KB));
      |                                                    ^
In file included from /usr/include/stdio.h:974,
                 from /builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/utils.c:7:
In function ‘snprintf’,
    inlined from ‘amount_to_str’ at /builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/utils.c:371:3:
/usr/include/bits/stdio2.h:68:10: note: ‘__snprintf_chk’ output between 4 and 7 bytes into a destination of size 6
   68 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   69 |                                    __glibc_objsize (__s), __fmt,
      |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   70 |                                    __va_arg_pack ());
      |                                    ~~~~~~~~~~~~~~~~~
/builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/utils.c: In function ‘amount_to_str’:
/builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/utils.c:369:52: warning: ‘__snprintf_chk’ output may be truncated before the last format character [-Wformat-truncation=]
  369 |                 snprintf(out, AMOUNT_STR_LEN, "%dMB", (int)((amount + M_MB - 1) / M_MB));
      |                                                    ^
In function ‘snprintf’,
    inlined from ‘amount_to_str’ at /builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/utils.c:369:3:
/usr/include/bits/stdio2.h:68:10: note: ‘__snprintf_chk’ output between 4 and 7 bytes into a destination of size 6
   68 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   69 |                                    __glibc_objsize (__s), __fmt,
      |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   70 |                                    __va_arg_pack ());
      |                                    ~~~~~~~~~~~~~~~~~
/builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/ui.c: In function ‘edit_color’:
/builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/ui.c:155:69: warning: ‘%d’ directive output may be truncated writing between 1 and 6 bytes into a region of size 5 [-Wformat-truncation=]
  155 |                                 snprintf(val_str, sizeof(val_str), "%d", val);
      |                                                                     ^~
/builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/ui.c:155:68: note: directive argument in the range [-32768, 32767]
  155 |                                 snprintf(val_str, sizeof(val_str), "%d", val);
      |                                                                    ^~~~
In file included from /usr/include/stdio.h:974,
                 from /builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/ui.c:5:
In function ‘snprintf’,
    inlined from ‘edit_color’ at /builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/ui.c:155:5:
/usr/include/bits/stdio2.h:68:10: note: ‘__snprintf_chk’ output between 2 and 7 bytes into a destination of size 5
   68 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   69 |                                    __glibc_objsize (__s), __fmt,
      |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   70 |                                    __va_arg_pack ());
      |                                    ~~~~~~~~~~~~~~~~~
/builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/ui.c: In function ‘set_window_sizes’:
/builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/ui.c:2168:83: warning: ‘%d’ directive output may be truncated writing between 1 and 10 bytes into a region of size 5 [-Wformat-truncation=]
 2168 |                                                 snprintf(oldval, sizeof(oldval), "%d", pi[window].win_height);
      |                                                                                   ^~
/builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/ui.c:2168:82: note: directive argument in the range [0, 2147483647]
 2168 |                                                 snprintf(oldval, sizeof(oldval), "%d", pi[window].win_height);
      |                                                                                  ^~~~
In function ‘snprintf’,
    inlined from ‘set_window_sizes’ at /builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/ui.c:2168:7:
/usr/include/bits/stdio2.h:68:10: note: ‘__snprintf_chk’ output between 2 and 11 bytes into a destination of size 5
   68 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   69 |                                    __glibc_objsize (__s), __fmt,
      |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   70 |                                    __va_arg_pack ());
      |                                    ~~~~~~~~~~~~~~~~~
/builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/ui.c: In function ‘edit_regexp’:
/builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/ui.c:1165:33: warning: ‘strncpy’ specified bound depends on the length of the source argument [-Wstringop-truncation]
 1165 |                                 strncpy(dummy, (cur -> pre)[loop].cmd, min(17, strlen((cur -> pre)[loop].cmd)));
      |                                 ^
In file included from /builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/ui.c:24:
/builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/ui.c:1165:80: note: length computed here
 1165 |                                 strncpy(dummy, (cur -> pre)[loop].cmd, min(17, strlen((cur -> pre)[loop].cmd)));

Increased buffer sizes in `utils.c` and `ui.c` to accommodate the full
range of integer values (preventing possible truncation).

Replaced `strncpy` with `snprintf` in `ui.c` to ensure destination
buffers are always null-terminated and free of garbage data.

Fixes the following warning:

/builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/utils.c: In function ‘amount_to_str’:
/builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/utils.c:371:52: warning: ‘__snprintf_chk’ output may be truncated before the last format character [-Wformat-truncation=]
  371 |                 snprintf(out, AMOUNT_STR_LEN, "%dKB", (int)((amount + M_KB - 1) / M_KB));
      |                                                    ^
In file included from /usr/include/stdio.h:974,
                 from /builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/utils.c:7:
In function ‘snprintf’,
    inlined from ‘amount_to_str’ at /builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/utils.c:371:3:
/usr/include/bits/stdio2.h:68:10: note: ‘__snprintf_chk’ output between 4 and 7 bytes into a destination of size 6
   68 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   69 |                                    __glibc_objsize (__s), __fmt,
      |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   70 |                                    __va_arg_pack ());
      |                                    ~~~~~~~~~~~~~~~~~
/builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/utils.c: In function ‘amount_to_str’:
/builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/utils.c:369:52: warning: ‘__snprintf_chk’ output may be truncated before the last format character [-Wformat-truncation=]
  369 |                 snprintf(out, AMOUNT_STR_LEN, "%dMB", (int)((amount + M_MB - 1) / M_MB));
      |                                                    ^
In function ‘snprintf’,
    inlined from ‘amount_to_str’ at /builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/utils.c:369:3:
/usr/include/bits/stdio2.h:68:10: note: ‘__snprintf_chk’ output between 4 and 7 bytes into a destination of size 6
   68 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   69 |                                    __glibc_objsize (__s), __fmt,
      |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   70 |                                    __va_arg_pack ());
      |                                    ~~~~~~~~~~~~~~~~~
/builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/ui.c: In function ‘edit_color’:
/builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/ui.c:155:69: warning: ‘%d’ directive output may be truncated writing between 1 and 6 bytes into a region of size 5 [-Wformat-truncation=]
  155 |                                 snprintf(val_str, sizeof(val_str), "%d", val);
      |                                                                     ^~
/builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/ui.c:155:68: note: directive argument in the range [-32768, 32767]
  155 |                                 snprintf(val_str, sizeof(val_str), "%d", val);
      |                                                                    ^~~~
In file included from /usr/include/stdio.h:974,
                 from /builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/ui.c:5:
In function ‘snprintf’,
    inlined from ‘edit_color’ at /builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/ui.c:155:5:
/usr/include/bits/stdio2.h:68:10: note: ‘__snprintf_chk’ output between 2 and 7 bytes into a destination of size 5
   68 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   69 |                                    __glibc_objsize (__s), __fmt,
      |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   70 |                                    __va_arg_pack ());
      |                                    ~~~~~~~~~~~~~~~~~
/builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/ui.c: In function ‘set_window_sizes’:
/builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/ui.c:2168:83: warning: ‘%d’ directive output may be truncated writing between 1 and 10 bytes into a region of size 5 [-Wformat-truncation=]
 2168 |                                                 snprintf(oldval, sizeof(oldval), "%d", pi[window].win_height);
      |                                                                                   ^~
/builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/ui.c:2168:82: note: directive argument in the range [0, 2147483647]
 2168 |                                                 snprintf(oldval, sizeof(oldval), "%d", pi[window].win_height);
      |                                                                                  ^~~~
In function ‘snprintf’,
    inlined from ‘set_window_sizes’ at /builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/ui.c:2168:7:
/usr/include/bits/stdio2.h:68:10: note: ‘__snprintf_chk’ output between 2 and 11 bytes into a destination of size 5
   68 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   69 |                                    __glibc_objsize (__s), __fmt,
      |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   70 |                                    __va_arg_pack ());
      |                                    ~~~~~~~~~~~~~~~~~
/builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/ui.c: In function ‘edit_regexp’:
/builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/ui.c:1165:33: warning: ‘strncpy’ specified bound depends on the length of the source argument [-Wstringop-truncation]
 1165 |                                 strncpy(dummy, (cur -> pre)[loop].cmd, min(17, strlen((cur -> pre)[loop].cmd)));
      |                                 ^
In file included from /builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/ui.c:24:
/builddir/build/BUILD/multitail-7.1.5-build/multitail-7.1.5/ui.c:1165:80: note: length computed here
 1165 |                                 strncpy(dummy, (cur -> pre)[loop].cmd, min(17, strlen((cur -> pre)[loop].cmd)));

Signed-off-by: Mikel Olasagasti Uranga <mikel@olasagasti.info>
@folkertvanheusden folkertvanheusden merged commit 3ebfb5b into folkertvanheusden:master Jan 2, 2026
2 checks passed
@folkertvanheusden

Copy link
Copy Markdown
Owner

Thank you for your pull requests!

@mikelolasagasti mikelolasagasti deleted the strncpy-warnings branch January 2, 2026 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants