[PATCH] Fix discarded const qualifier warnings

29 views
Skip to first unread message

Jere Viikari

unread,
Jun 12, 2026, 11:45:13 AMJun 12
to tmux-...@googlegroups.com
Hello,

I tried building current tmux with GCC 16 and got seven warnings about
discarded const qualifiers. The pointers in these places are only used for
reading, so the fixes are quite trivial: mark them const and propagate the
const return type in format_table_get().

For example:
  compat/getopt_long.c:472:18: warning: assignment discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]

I tested the patch by doing clean builds with:
  GCC 16.0.1 20260322 (experimental)
  Clang 22.1.2

The GCC build is clean after the patch. The Clang build also succeeds but
still prints some unrelated existing warnings about unsupported warning
options and setproctitle. I also ran make check and git diff --check.

Regards,
Jere

--

diff --git a/compat/getopt_long.c b/compat/getopt_long.c
index 3d4df3fc..0429a035 100644
--- a/compat/getopt_long.c
+++ b/compat/getopt_long.c
@@ -339,7 +339,7 @@ static int
 getopt_internal(int nargc, char * const *nargv, const char *options,
  const struct option *long_options, int *idx, int flags)
 {
- char *oli; /* option letter list index */
+ const char *oli; /* option letter list index */
  int optchar, short_too;
  static int posixly_correct = -1;

diff --git a/environ.c b/environ.c
index 68e0417b..19f9c327 100644
--- a/environ.c
+++ b/environ.c
@@ -152,7 +152,8 @@ environ_clear(struct environ *env, const char *name)
 void
 environ_put(struct environ *env, const char *var, int flags)
 {
- char *name, *value;
+ char *name;
+ const char *value;

  value = strchr(var, '=');
  if (value == NULL)
diff --git a/format.c b/format.c
index 129ad582..0078555d 100644
--- a/format.c
+++ b/format.c
@@ -3775,7 +3775,7 @@ format_table_compare(const void *key0, const void *entry0)
 }

 /* Get a format callback. */
-static struct format_table_entry *
+static const struct format_table_entry *
 format_table_get(const char *key)
 {
  return (bsearch(key, format_table, nitems(format_table),
@@ -4113,7 +4113,7 @@ static char *
 format_find(struct format_tree *ft, const char *key, int modifiers,
     const char *time_format)
 {
- struct format_table_entry *fte;
+ const struct format_table_entry *fte;
  void *value;
  struct format_entry *fe, fe_find;
  struct environ_entry *envent;
diff --git a/input-keys.c b/input-keys.c
index fdce5409..662b4214 100644
--- a/input-keys.c
+++ b/input-keys.c
@@ -483,7 +483,7 @@ input_key_vt10x(struct bufferevent *bev, key_code key)
 {
  struct utf8_data ud;
  key_code onlykey;
- char *p;
+ const char *p;
  static const char *standard_map[2] = {
  "1!9(0)=+;:'\",<.>/-8? 2",
  "119900=+;;'',,..\x1f\x1f\x7f\x7f\0\0",
diff --git a/input.c b/input.c
index 6c9acd07..7e808c50 100644
--- a/input.c
+++ b/input.c
@@ -1365,7 +1365,7 @@ input_esc_dispatch(struct input_ctx *ictx)
 {
  struct screen_write_ctx *sctx = &ictx->ctx;
  struct screen *s = sctx->s;
- struct input_table_entry *entry;
+ const struct input_table_entry *entry;

  if (ictx->flags & INPUT_DISCARD)
  return (0);
@@ -1441,7 +1441,7 @@ input_csi_dispatch(struct input_ctx *ictx)
 {
  struct screen_write_ctx        *sctx = &ictx->ctx;
  struct screen        *s = sctx->s;
- struct input_table_entry       *entry;
+ const struct input_table_entry *entry;
  struct options        *oo;
  int i, n, m, ek, set, p;
  u_int cx, bg = ictx->cell.cell.bg;
@@ -3225,7 +3225,7 @@ static int
 input_osc_52_parse(struct input_ctx *ictx, const char *p, u_char **out,
     int *outlen, char *clip)
 {
- char *end;
+ const char *end;
  size_t len;
  const char *allow = "cpqs01234567";
  u_int i, j = 0;

Nicholas Marriott

unread,
Jun 12, 2026, 11:59:06 AMJun 12
to Jere Viikari, tmux-...@googlegroups.com
Did you change the compiler flags? bsearch and strchr return char * in C99...



--
You received this message because you are subscribed to the Google Groups "tmux-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tmux-users+...@googlegroups.com.
To view this discussion, visit https://groups.google.com/d/msgid/tmux-users/CALDd4YUOZ247q6-C6X4zhj%2B-oukChnrhNLFMNb08-BwrwyB3fQ%40mail.gmail.com.

Jere Viikari

unread,
Jun 12, 2026, 12:37:01 PMJun 12
to Nicholas Marriott, tmux-...@googlegroups.com
I might have some aggressively optimized compiler flags:
export CFLAGS="\
  -O3 \
  -pipe \
  -march=native \
  -mtune=native \
  -fomit-frame-pointer \
  -fno-semantic-interposition \
  -fipa-pta \
"
They'll outputs these warnings:

environ.c: In function ‘environ_put’:
environ.c:157:15: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
  157 |         value = strchr(var, '=');
      |               ^
input-keys.c: In function ‘input_key_vt10x’:
input-keys.c:525:19: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
  525 |                 p = strchr(standard_map[0], onlykey);
      |                   ^
input.c: In function ‘input_esc_dispatch’:
input.c:1374:15: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
 1374 |         entry = bsearch(ictx, input_esc_table, nitems(input_esc_table),
      |               ^
input.c: In function ‘input_csi_dispatch’:
input.c:1458:15: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
 1458 |         entry = bsearch(ictx, input_csi_table, nitems(input_csi_table),
      |               ^
format.c: In function ‘format_table_get’:
format.c:3781:17: warning: return discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
 3781 |         return (bsearch(key, format_table, nitems(format_table),
      |                 ^~~~~~~
input.c: In function ‘input_osc_52_parse’:
input.c:3236:18: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
 3236 |         if ((end = strchr(p, ';')) == NULL)
      |                  ^
compat/getopt_long.c: In function ‘getopt_internal’:

compat/getopt_long.c:472:18: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
  472 |             (oli = strchr(options, optchar)) == NULL) {
      | 

BR, Jere

Nicholas Marriott

unread,
Jun 12, 2026, 12:39:09 PMJun 12
to Jere Viikari, tmux-...@googlegroups.com
You have probably ending up removing -std=gnu99

There is probably no harm in making these changes anyway, I'll take a look later

Thanks

Jere Viikari

unread,
Jun 12, 2026, 1:03:17 PMJun 12
to Nicholas Marriott, tmux-...@googlegroups.com
This does not seem to be specifically a GCC 16 change. I got the same warnings from a clean build without them.

Tmux is still compiled with -std=gnu99. The warnings appear because newer glibc (I have Ubuntu GLIBC 2.43-2ubuntu2) headers provide const-preserving definitions for functions such as strchr when _GNU_SOURCE is enabled. GCC then notices that their results are assigned to non-const pointers.

The patches should still be safe and backward compatible.
I agree, this is not urgent at all.

BR, Jere

Nicholas Marriott

unread,
Jun 12, 2026, 1:04:09 PMJun 12
to Jere Viikari, tmux-users
It seems like a toolchain bug then because in C99 strchr and bsearch both return char *.

Nicholas Marriott

unread,
Jun 12, 2026, 1:05:03 PMJun 12
to Jere Viikari, tmux-users
Oh yeah _GNU_SOURCE/gnu99 they probably just say this is an extension.

Well we can change it anyway.

Jere Viikari

unread,
Jun 12, 2026, 1:17:02 PMJun 12
to Nicholas Marriott, tmux-users
Yes, toolchain one.

const-preserving change was introduced in glibc 2.43 by this commit:
https://github.com/bminor/glibc/commit/cd748a63ab1a7ae846175c532a3daab341c62690

BR, Jere

Nicholas Marriott

unread,
Jun 13, 2026, 9:00:43 AMJun 13
to Jere Viikari, tmux-...@googlegroups.com
It looks like your email client has mangled the patch. Could you please send it as an attachment?



On Fri, 12 Jun 2026 at 16:45, Jere Viikari <jere.v...@gmail.com> wrote:
--

Jere Viikari

unread,
Jun 13, 2026, 3:00:51 PMJun 13
to Nicholas Marriott, tmux-...@googlegroups.com

Nicholas Marriott

unread,
Jun 13, 2026, 4:08:13 PMJun 13
to Jere Viikari, tmux-...@googlegroups.com
Applied now, thanks.

(FWIW this fixed the line wrapping but still mangled all the tabs, I just fixed them up manually.)

Jere Viikari

unread,
Jun 13, 2026, 4:27:21 PMJun 13
to Nicholas Marriott, tmux-...@googlegroups.com

Thanks.

Gmail does not support tabs even in plain text mode. Need to take that into account.

Jere Viikari

unread,
Jun 13, 2026, 5:03:34 PMJun 13
to Nicholas Marriott, tmux-...@googlegroups.com
This was missing in the patch. Maybe I forgotten run make clean or
something similar.
It is just oneliner so I do not attach patch file.

diff --git a/compat/getopt_long.c b/compat/getopt_long.c
index 3d4df3fc..xxxxxxxx 100644
--- a/compat/getopt_long.c
+++ b/compat/getopt_long.c
@@ -339,7 +339,7 @@ getopt_internal(int nargc, char * const *nargv,
const char *options,
{
char *oli; /* option letter list index */
+ const char *oli; /* option letter list index */

Nicholas Marriott

unread,
Jun 13, 2026, 5:04:18 PMJun 13
to Jere Viikari, tmux-users
my fault but this came from somewhere else so i will need to check where that was (might have been openssh)

Nicholas Marriott

unread,
Jun 14, 2026, 4:52:28 AMJun 14
to Jere Viikari, tmux-...@googlegroups.com
applied this now thanks

Reply all
Reply to author
Forward
0 new messages