fix: relayd: unaligned access in trace_chunk_registry_ht_key_hash
[lttng-tools.git] / src / common / string-utils / string-utils.cpp
index 8dcc6b2aae9ce9fab7b3beeefbea0b495fc15db4..4f727e2e8ed0fe9f407249e538a4dfe1b80d7f85 100644 (file)
@@ -1,20 +1,22 @@
 /*
  * Copyright (C) 2017 Philippe Proulx <pproulx@efficios.com>
  *
- * SPDX-License-Identifier: GPL-2.0-only
+ * SPDX-License-Identifier: LGPL-2.1-only
  *
  */
 
 #define _LGPL_SOURCE
+#include "../macros.hpp"
+#include "string-utils.hpp"
+
+#include <assert.h>
+#include <errno.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <stdbool.h>
 #include <type_traits>
-#include <assert.h>
-#include <errno.h>
-
-#include "string-utils.h"
-#include "../macros.h"
 
 enum star_glob_pattern_type_flags {
        STAR_GLOB_PATTERN_TYPE_FLAG_NONE = 0,
@@ -22,13 +24,11 @@ enum star_glob_pattern_type_flags {
        STAR_GLOB_PATTERN_TYPE_FLAG_END_ONLY = 2,
 };
 
-static
-star_glob_pattern_type_flags &operator|=(star_glob_pattern_type_flags &l,
-               star_glob_pattern_type_flags r)
+static star_glob_pattern_type_flags& operator|=(star_glob_pattern_type_flags& l,
+                                               star_glob_pattern_type_flags r)
 {
        using T = std::underlying_type<star_glob_pattern_type_flags>::type;
-       l = static_cast<star_glob_pattern_type_flags> (
-               static_cast<T> (l) | static_cast<T> (r));
+       l = static_cast<star_glob_pattern_type_flags>(static_cast<T>(l) | static_cast<T>(r));
        return l;
 }
 
@@ -64,7 +64,7 @@ void strutils_normalize_star_glob_pattern(char *pattern)
                                goto end;
                        }
 
-                       /* Fall through default case. */
+                       /* fall through */
                default:
                        got_star = false;
                        break;
@@ -79,11 +79,9 @@ end:
        *np = '\0';
 }
 
-static
-enum star_glob_pattern_type_flags strutils_test_glob_pattern(const char *pattern)
+static enum star_glob_pattern_type_flags strutils_test_glob_pattern(const char *pattern)
 {
-       enum star_glob_pattern_type_flags ret =
-               STAR_GLOB_PATTERN_TYPE_FLAG_NONE;
+       enum star_glob_pattern_type_flags ret = STAR_GLOB_PATTERN_TYPE_FLAG_NONE;
        const char *p;
 
        LTTNG_ASSERT(pattern);
@@ -120,8 +118,7 @@ end:
  */
 bool strutils_is_star_glob_pattern(const char *pattern)
 {
-       return strutils_test_glob_pattern(pattern) &
-               STAR_GLOB_PATTERN_TYPE_FLAG_PATTERN;
+       return strutils_test_glob_pattern(pattern) & STAR_GLOB_PATTERN_TYPE_FLAG_PATTERN;
 }
 
 /*
@@ -130,8 +127,7 @@ bool strutils_is_star_glob_pattern(const char *pattern)
  */
 bool strutils_is_star_at_the_end_only_glob_pattern(const char *pattern)
 {
-       return strutils_test_glob_pattern(pattern) &
-               STAR_GLOB_PATTERN_TYPE_FLAG_END_ONLY;
+       return strutils_test_glob_pattern(pattern) & STAR_GLOB_PATTERN_TYPE_FLAG_END_ONLY;
 }
 
 /*
@@ -146,7 +142,7 @@ char *strutils_unescape_string(const char *input, char only_char)
        const char *i;
 
        LTTNG_ASSERT(input);
-       output = (char *) zmalloc(strlen(input) + 1);
+       output = calloc<char>(strlen(input) + 1);
        if (!output) {
                goto end;
        }
@@ -249,9 +245,9 @@ void strutils_free_null_terminated_array_of_strings(char **array)
  * Returns -1 if there's an error.
  */
 int strutils_split(const char *input,
-               char delim,
-               bool escape_delim,
-               struct lttng_dynamic_pointer_array *out_strings)
+                  char delim,
+                  bool escape_delim,
+                  struct lttng_dynamic_pointer_array *out_strings)
 {
        int ret;
        size_t at;
@@ -297,14 +293,13 @@ int strutils_split(const char *input,
        for (at = 0, s = input; at < number_of_substrings; at++) {
                const char *ss;
                char *d;
-               char *substring = (char *) zmalloc(longest_substring_len + 1);
+               char *substring = calloc<char>(longest_substring_len + 1);
 
                if (!substring) {
                        goto error;
                }
 
-               ret = lttng_dynamic_pointer_array_add_pointer(
-                               out_strings, substring);
+               ret = lttng_dynamic_pointer_array_add_pointer(out_strings, substring);
                if (ret) {
                        free(substring);
                        goto error;
@@ -362,9 +357,9 @@ end:
        return ret;
 }
 
-size_t strutils_array_of_strings_len(char * const *array)
+size_t strutils_array_of_strings_len(char *const *array)
 {
-       char * const *item;
+       char *const *item;
        size_t count = 0;
 
        LTTNG_ASSERT(array);
@@ -380,10 +375,10 @@ int strutils_append_str(char **s, const char *append)
 {
        char *old = *s;
        char *new_str;
-       size_t oldlen = (old == NULL) ? 0 : strlen(old);
+       size_t oldlen = (old == nullptr) ? 0 : strlen(old);
        size_t appendlen = strlen(append);
 
-       new_str = (char *) zmalloc(oldlen + appendlen + 1);
+       new_str = zmalloc<char>(oldlen + appendlen + 1);
        if (!new_str) {
                return -ENOMEM;
        }
@@ -395,3 +390,51 @@ int strutils_append_str(char **s, const char *append)
        free(old);
        return 0;
 }
+
+int strutils_appendf(char **s, const char *fmt, ...)
+{
+       char *new_str;
+       size_t oldlen = (*s) ? strlen(*s) : 0;
+       size_t addlen = 0;
+       int ret;
+       va_list args;
+
+       /* Compute length of formatted string we append. */
+       va_start(args, fmt);
+       ret = vsnprintf(nullptr, 0, fmt, args);
+       va_end(args);
+
+       if (ret == -1) {
+               goto end;
+       }
+
+       /* Allocate space for old string + new string + \0. */
+       addlen = ret + 1;
+       new_str = zmalloc<char>(oldlen + addlen);
+       if (!new_str) {
+               ret = -ENOMEM;
+               goto end;
+       }
+
+       /* Copy old string, if there was one. */
+       if (oldlen) {
+               strcpy(new_str, *s);
+       }
+
+       /* Format new string in-place. */
+       va_start(args, fmt);
+       ret = vsnprintf(&new_str[oldlen], addlen, fmt, args);
+       va_end(args);
+
+       if (ret == -1) {
+               ret = -1;
+               goto end;
+       }
+
+       free(*s);
+       *s = new_str;
+       new_str = nullptr;
+
+end:
+       return ret;
+}
This page took 0.025977 seconds and 4 git commands to generate.