Fix memory leak in get random string
[lttng-tools.git] / tests / test_kernel_data_trace.c
index bfc0f41afd2ce320c32fe26766aee4a7150b118f..0a534699c5444552669f7c0d9043a528d6579949 100644 (file)
@@ -33,6 +33,8 @@
 /* This path will NEVER be created in this test */
 #define PATH1 "/tmp/.test-junk-lttng"
 
+#define RANDOM_STRING_LEN      11
+
 /* For lttngerr.h */
 int opt_quiet = 1;
 int opt_verbose = 0;
@@ -43,22 +45,23 @@ static const char alphanum[] =
        "abcdefghijklmnopqrstuvwxyz";
 
 static struct ltt_kernel_session *kern;
+static char random_string[RANDOM_STRING_LEN];
 
 /*
  * Return random string of 10 characters.
+ * Not thread-safe.
  */
 static char *get_random_string(void)
 {
        int i;
-       char *str = malloc(11);
 
-       for (i = 0; i < 10; i++) {
-               str[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
+       for (i = 0; i < RANDOM_STRING_LEN - 1; i++) {
+               random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
        }
 
-       str[10] = '\0';
+       random_string[RANDOM_STRING_LEN - 1] = '\0';
 
-       return str;
+       return random_string;
 }
 
 static void create_one_kernel_session(void)
This page took 0.025138 seconds and 5 git commands to generate.