File 1143-erts-Fix-Wformat-2-warnings.patch of Package erlang
From a152e03a64f6d03f97baaa6a370330fe4e47f5f2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lukas=20Backstr=C3=B6m?= <lukas@erlang.org>
Date: Mon, 28 Oct 2024 09:34:44 +0100
Subject: [PATCH 03/10] erts: Fix -Wformat=2 warnings
---
erts/config.h.in | 4 ++
erts/configure | 24 ++++++++++
erts/configure.ac | 11 +++++
erts/etc/unix/run_erl.c | 21 +++++++--
erts/lib_src/common/erl_misc_utils.c | 61 ++++++++++++++-----------
erts/lib_src/common/erl_printf_format.c | 7 +++
6 files changed, 98 insertions(+), 30 deletions(-)
diff --git a/erts/config.h.in b/erts/config.h.in
index 733e184340..8a1d0cdb5c 100644
--- a/erts/config.h.in
+++ b/erts/config.h.in
@@ -672,6 +672,10 @@
'-Waddress-of-packed-member'') */
#undef HAVE_GCC_DIAG_IGNORE_WADDRESS_OF_PACKED_MEMBER
+/* define if compiler support _Pragma('GCC diagnostic ignored
+ '-Wformat-nonliteral'') */
+#undef HAVE_GCC_DIAG_IGNORE_WFORMAT_NONLITERAL
+
/* Define to 1 if you have a good `getaddrinfo' function. */
#undef HAVE_GETADDRINFO
diff --git a/erts/configure b/erts/configure
index 98efb385c1..0459e638bc 100755
--- a/erts/configure
+++ b/erts/configure
@@ -26895,6 +26895,30 @@ fi
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
CFLAGS="$saved_CFLAGS"
+saved_CFLAGS="$CFLAGS"
+CFLAGS="-Werror $CFLAGS"
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h. */
+
+int
+main (void)
+{
+_Pragma("GCC diagnostic push")
+ _Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\"")
+ _Pragma("GCC diagnostic pop")
+
+ ;
+ return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"
+then :
+
+printf "%s\n" "#define HAVE_GCC_DIAG_IGNORE_WFORMAT_NONLITERAL 1" >>confdefs.h
+
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.beam conftest.$ac_ext
+CFLAGS="$saved_CFLAGS"
if test "x$GCC" = xyes; then
diff --git a/erts/configure.in b/erts/configure.in
index e79def7c42..5bca25bb0b 100644
--- a/erts/configure.in
+++ b/erts/configure.in
@@ -3621,6 +3621,17 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[_Pragma("GCC diagnostic push")
[define if compiler support _Pragma('GCC diagnostic ignored '-Waddress-of-packed-member'')]))
CFLAGS="$saved_CFLAGS"
+dnl ----------------------------------------------------------------------
+dnl Check for GCC diagnostic ignored "-Wformat-nonliteral"
+dnl ----------------------------------------------------------------------
+saved_CFLAGS="$CFLAGS"
+CFLAGS="-Werror $CFLAGS"
+AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[_Pragma("GCC diagnostic push")
+ _Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\"")
+ _Pragma("GCC diagnostic pop")
+ ]])],[AC_DEFINE(HAVE_GCC_DIAG_IGNORE_WFORMAT_NONLITERAL,1,
+ define if compiler support _Pragma('GCC diagnostic ignored '-Wformat-nonliteral''))],[])
+CFLAGS="$saved_CFLAGS"
dnl ----------------------------------------------------------------------
dnl Enable any -Werror flags
diff --git a/erts/etc/unix/run_erl.c b/erts/etc/unix/run_erl.c
index 6157646630..b0966de605 100644
--- a/erts/etc/unix/run_erl.c
+++ b/erts/etc/unix/run_erl.c
@@ -488,6 +488,21 @@ int main(int argc, char **argv)
return 0;
} /* main() */
+/* Broken out in order to do GCC diagnostic ignore here */
+#ifdef HAVE_GCC_DIAG_IGNORE_WFORMAT_NONLITERAL
+_Pragma("GCC diagnostic push");
+_Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\"");
+#endif
+static int dynamic_strftime(
+ char *__restrict__ log_alive_buffer,
+ const char *__restrict__ log_alive_format,
+ const struct tm *__restrict__ tmptr) {
+ return strftime(log_alive_buffer, ALIVE_BUFFSIZ, log_alive_format, tmptr);
+}
+#ifdef HAVE_GCC_DIAG_IGNORE_WFORMAT_NONLITERAL
+_Pragma("GCC diagnostic pop");
+#endif
+
/* pass_on()
* Is the work loop of the logger. Selects on the pipe to the to_erl
* program erlang. If input arrives from to_erl it is passed on to
@@ -584,8 +599,7 @@ static void pass_on(pid_t childpid)
} else {
tmptr = localtime(&now);
}
- if (!strftime(log_alive_buffer, ALIVE_BUFFSIZ, log_alive_format,
- tmptr)) {
+ if (!dynamic_strftime(log_alive_buffer, log_alive_format, tmptr)) {
strn_cpy(log_alive_buffer, sizeof(log_alive_buffer),
"(could not format time in 256 positions "
"with current format string.)");
@@ -858,8 +872,7 @@ static int open_log(int log_num, int flags)
} else {
tmptr = localtime(&now);
}
- if (!strftime(log_buffer, ALIVE_BUFFSIZ, log_alive_format,
- tmptr)) {
+ if (!dynamic_strftime(log_buffer, log_alive_format, tmptr)) {
strn_cpy(log_buffer, sizeof(log_buffer),
"(could not format time in 256 positions "
"with current format string.)");
diff --git a/erts/lib_src/common/erl_misc_utils.c b/erts/lib_src/common/erl_misc_utils.c
index 01f8b35ffe..74941ce570 100644
--- a/erts/lib_src/common/erl_misc_utils.c
+++ b/erts/lib_src/common/erl_misc_utils.c
@@ -1124,7 +1124,6 @@ get_cgroup_path(const char *controller,
const char **out) {
enum cgroup_version_t version;
char mount_line[10 << 10];
- const char *mount_format;
const char *child_path;
FILE *mount_file;
@@ -1134,27 +1133,10 @@ get_cgroup_path(const char *controller,
}
version = get_cgroup_child_path(controller, &child_path);
- switch (version) {
- case ERTS_CGROUP_NONE:
+
+ if (version == ERTS_CGROUP_NONE) {
fclose(mount_file);
return ERTS_CGROUP_NONE;
- case ERTS_CGROUP_V1:
- /* Format:
- * [Mount id] [Parent id] [Major] [Minor] [Root] [Mounted at] \
- * [Mount flags] ... (options terminated by a single hyphen) ... \
- * [FS type] [Mount source] [Flags]
- *
- * (See proc(5) for a more complete description.)
- *
- * This fails if any of the fs options contain a hyphen, but this is
- * not likely to happen on a cgroup, so we just skip such lines. */
- mount_format = "%*d %*d %*d:%*d %4095s %4095s %*s%*[^-]- "
- "cgroup %*s %511[^\n]\n";
- break;
- case ERTS_CGROUP_V2:
- mount_format = "%*d %*d %*d:%*d %4095s %4095s %*s%*[^-]- "
- "cgroup2 %*s %511[^\n]\n";
- break;
}
/* As a controller can only belong to one hierarchy, regardless of
@@ -1166,12 +1148,39 @@ get_cgroup_path(const char *controller,
char root_path[4 << 10];
char fs_flags[512];
- if (sscanf(mount_line,
- mount_format,
- root_path,
- mount_path,
- fs_flags) != 3) {
- continue;
+ switch (version) {
+
+ case ERTS_CGROUP_V1:
+ /* Format:
+ * [Mount id] [Parent id] [Major] [Minor] [Root] [Mounted at] \
+ * [Mount flags] ... (options terminated by a single hyphen) ... \
+ * [FS type] [Mount source] [Flags]
+ *
+ * (See proc(5) for a more complete description.)
+ *
+ * This fails if any of the fs options contain a hyphen, but this is
+ * not likely to happen on a cgroup, so we just skip such lines. */
+ if (sscanf(mount_line,
+ "%*d %*d %*d:%*d %4095s %4095s %*s%*[^-]- "
+ "cgroup %*s %511[^\n]\n",
+ root_path,
+ mount_path,
+ fs_flags) != 3) {
+ continue;
+ }
+ break;
+ case ERTS_CGROUP_V2:
+ if (sscanf(mount_line,
+ "%*d %*d %*d:%*d %4095s %4095s %*s%*[^-]- "
+ "cgroup2 %*s %511[^\n]\n",
+ root_path,
+ mount_path,
+ fs_flags) != 3) {
+ continue;
+ }
+ break;
+ default:
+ ASSERT(0 && "Only V1 and V2 should come here");
}
if (version == ERTS_CGROUP_V2) {
diff --git a/erts/lib_src/common/erl_printf_format.c b/erts/lib_src/common/erl_printf_format.c
index 9449e9f26a..e854825a2a 100644
--- a/erts/lib_src/common/erl_printf_format.c
+++ b/erts/lib_src/common/erl_printf_format.c
@@ -426,7 +426,14 @@ static int fmt_double(fmtfn_t fn,void*arg,double val,
}
}
+#ifdef HAVE_GCC_DIAG_IGNORE_WFORMAT_NONLITERAL
+_Pragma("GCC diagnostic push");
+_Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\"");
+#endif
size = sprintf(bufp, format_str, precision, val);
+#ifdef HAVE_GCC_DIAG_IGNORE_WFORMAT_NONLITERAL
+_Pragma("GCC diagnostic pop");
+#endif
if (size < 0) {
if (errno > 0)
res = -errno;
--
2.43.0