[PATCH v7 06/14] linux/termios: make pad field in struct termios explicit and zero it

H. Peter Anvin hpa@zytor.com
Sun Jun 8 02:19:09 GMT 2025


There is a 3-byte padding field in struct termios due to NCCS being
set to 32 and there being one more byte-sized variable next to it
(c_line). As this is then aligned to a 4-byte boundary, there is an
implicit 3-byte field.

Make this implicit 3-byte field explicit, and make sure it is zeroed
in the relevant places (tcgetattr() and __old_tcsetattr()). This might
make future extensions easier, plus, it is bad practice to leave bytes
uninitialized by an API.

Signed-off-by: "H. Peter Anvin" (Intel) <hpa@zytor.com>
---
 sysdeps/unix/sysv/linux/bits/termios-struct.h | 3 ++-
 sysdeps/unix/sysv/linux/tcgetattr.c           | 1 +
 sysdeps/unix/sysv/linux/tcsetattr.c           | 4 ++--
 3 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/sysdeps/unix/sysv/linux/bits/termios-struct.h b/sysdeps/unix/sysv/linux/bits/termios-struct.h
index 4c501a54b058..f0076bfeeebf 100644
--- a/sysdeps/unix/sysv/linux/bits/termios-struct.h
+++ b/sysdeps/unix/sysv/linux/bits/termios-struct.h
@@ -27,8 +27,9 @@ struct termios
     tcflag_t c_oflag;		/* output mode flags */
     tcflag_t c_cflag;		/* control mode flags */
     tcflag_t c_lflag;		/* local mode flags */
-    cc_t c_line;			/* line discipline */
+    cc_t c_line;		/* line discipline */
     cc_t c_cc[NCCS];		/* control characters */
+    cc_t __reserved[3];		/* reserved (was padding), should be zero */
     speed_t c_ispeed;		/* input speed */
     speed_t c_ospeed;		/* output speed */
 #define _HAVE_STRUCT_TERMIOS_C_ISPEED 1
diff --git a/sysdeps/unix/sysv/linux/tcgetattr.c b/sysdeps/unix/sysv/linux/tcgetattr.c
index dbeae739cc31..7eed18b74b7d 100644
--- a/sysdeps/unix/sysv/linux/tcgetattr.c
+++ b/sysdeps/unix/sysv/linux/tcgetattr.c
@@ -28,6 +28,7 @@ __tcgetattr (int fd, struct termios *termios_p)
     {
       ___termios2_canonicalize_speeds (&k_termios);
 
+      memset (termios_p, 0, sizeof (*termios_p));
       termios_p->c_iflag  = k_termios.c_iflag;
       termios_p->c_oflag  = k_termios.c_oflag;
       termios_p->c_cflag  = k_termios.c_cflag;
diff --git a/sysdeps/unix/sysv/linux/tcsetattr.c b/sysdeps/unix/sysv/linux/tcsetattr.c
index 7ec53e41937f..ae4361a22bd3 100644
--- a/sysdeps/unix/sysv/linux/tcsetattr.c
+++ b/sysdeps/unix/sysv/linux/tcsetattr.c
@@ -88,13 +88,13 @@ attribute_compat_text_section
 __old_tcsetattr (int fd, int optional_actions, const old_termios_t *termios_p)
 {
   struct termios new_termios;
+
+  memset (&new_termios, 0, sizeof (new_termios));
   new_termios.c_iflag  = termios_p->c_iflag;
   new_termios.c_oflag  = termios_p->c_oflag;
   new_termios.c_cflag  = termios_p->c_cflag;
   new_termios.c_lflag  = termios_p->c_lflag;
   new_termios.c_line   = termios_p->c_line;
-  new_termios.c_ispeed = 0;
-  new_termios.c_ospeed = 0;
   copy_c_cc(new_termios.c_cc, NCCS, termios_p->c_cc, OLD_NCCS);
 
   return __tcsetattr (fd, optional_actions, &new_termios);
-- 
2.49.0



More information about the Libc-alpha mailing list