lttng: remove use of variable length array
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 6 Sep 2024 21:39:23 +0000 (21:39 +0000)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Mon, 9 Sep 2024 14:09:46 +0000 (10:09 -0400)
Use fmtlib to format the session attribute string when saving
the current session to .lttngrc. This eliminates a warning
emitted by clang (VLAs are not standard in C++).

Change-Id: Icdb8c1cc47adcbdfd82eefa8d2f1bf37a042a028
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
src/bin/lttng/conf.cpp

index 81aa654384c97bbccf0360be39d91a2e7326f230..0b521fc324f01ce8d1b4a9823864d007e28c1893 100644 (file)
@@ -90,10 +90,10 @@ error:
  * On success, returns 0;
  * on error, returns -1.
  */
-static int write_config(const char *file_path, size_t size, char *data)
+static int write_config(const char *file_path, std::size_t size, const char *data)
 {
        FILE *fp;
-       size_t len;
+       std::size_t len;
        int ret = 0;
 
        fp = open_config(file_path, "a");
@@ -266,23 +266,16 @@ char *config_read_session_name_quiet(const char *path)
  */
 int config_add_session_name(const char *path, const char *name)
 {
-       int ret;
-       const char *attr = "session=";
-       /* Max name len accepted plus attribute's len and the NULL byte. */
-       char session_name[NAME_MAX + strlen(attr) + 1];
-
-       /*
-        * With GNU C <  2.1, snprintf returns -1 if the target buffer is too small;
-        * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
-        */
-       ret = snprintf(session_name, sizeof(session_name), "%s%s\n", attr, name);
-       if (ret < 0) {
-               ret = -1;
-               goto error;
+       std::string attribute;
+       try {
+               attribute = fmt::format("session={}", name);
+       } catch (const std::exception& ex) {
+               ERR_FMT("Failed to format session name attribute for configuration file: {}",
+                       ex.what());
+               return -1;
        }
-       ret = write_config(path, ret, session_name);
-error:
-       return ret;
+
+       return write_config(path, attribute.size(), attribute.c_str());
 }
 
 /*
This page took 0.025744 seconds and 4 git commands to generate.