From 3c027268a4fe0dab76b2b661263a67eb676cabbd Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Mon, 19 Apr 2021 19:22:33 +0100 Subject: [PATCH 1/3] dirent: Simplify a memset() call Let the compiler figure out the size, rather than hard-coding it the same as the struct definition. This makes no functional changes. Signed-off-by: Philip Withnall --- glib/dirent/dirent.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/glib/dirent/dirent.c b/glib/dirent/dirent.c index 26b6cb1e54..5b8f3ff809 100644 --- a/glib/dirent/dirent.c +++ b/glib/dirent/dirent.c @@ -135,7 +135,7 @@ _topendir (const _TCHAR *szPath) nd->dd_dir.d_ino = 0; nd->dd_dir.d_reclen = 0; nd->dd_dir.d_namlen = 0; - memset (nd->dd_dir.d_name, 0, FILENAME_MAX); + memset (nd->dd_dir.d_name, 0, sizeof (nd->dd_dir.d_name)); return nd; } -- GitLab From 496135c569788334b4256c72e781cad7dbc9404a Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Mon, 19 Apr 2021 19:23:10 +0100 Subject: [PATCH 2/3] dirent: Add trailing nul delimiter to FILENAME_MAX `FILENAME_MAX` is not guaranteed to include a trailing nul delimiter, so explicitly add one. See https://www.gnu.org/software/libc/manual/html_node/Limits-for-Files.html: `PATH_MAX` is explicitly defined to include a nul delimiter; `FILENAME_MAX` is not. See also https://twitter.com/pid_eins/status/1357008940886818822. Signed-off-by: Philip Withnall --- glib/dirent/dirent.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/glib/dirent/dirent.h b/glib/dirent/dirent.h index 237665b4ed..857710f6a1 100644 --- a/glib/dirent/dirent.h +++ b/glib/dirent/dirent.h @@ -22,7 +22,7 @@ struct dirent long d_ino; /* Always zero. */ unsigned short d_reclen; /* Always zero. */ unsigned short d_namlen; /* Length of name in d_name. */ - char d_name[FILENAME_MAX]; /* File name. */ + char d_name[FILENAME_MAX+1]; /* File name plus nul delimiter. */ }; #ifdef _WIN64 @@ -76,7 +76,7 @@ struct _wdirent long d_ino; /* Always zero. */ unsigned short d_reclen; /* Always zero. */ unsigned short d_namlen; /* Length of name in d_name. */ - wchar_t d_name[FILENAME_MAX]; /* File name. */ + wchar_t d_name[FILENAME_MAX+1]; /* File name plus nul delimiter. */ }; /* -- GitLab From 3131a6149e71208e5e5f077ed42fd6254b115a93 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Mon, 19 Apr 2021 19:24:08 +0100 Subject: [PATCH 3/3] gdir: Add trailing nul delimiter to FILENAME_MAX `FILENAME_MAX` is not guaranteed to include a trailing nul delimiter, so explicitly add one. See https://www.gnu.org/software/libc/manual/html_node/Limits-for-Files.html: `PATH_MAX` is explicitly defined to include a nul delimiter; `FILENAME_MAX` is not. See also https://twitter.com/pid_eins/status/1357008940886818822. Signed-off-by: Philip Withnall --- glib/gdir.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/glib/gdir.c b/glib/gdir.c index c26edc1dc0..b07eb5c206 100644 --- a/glib/gdir.c +++ b/glib/gdir.c @@ -60,7 +60,9 @@ struct _GDir DIR *dirp; #endif #ifdef G_OS_WIN32 - gchar utf8_buf[FILENAME_MAX*4]; + /* maximum encoding of FILENAME_MAX UTF-8 characters, plus a nul terminator + * (FILENAME_MAX is not guaranteed to include one) */ + gchar utf8_buf[FILENAME_MAX*4 + 1]; #endif }; -- GitLab