Fix memory leak in get random string
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 7 Feb 2012 02:59:29 +0000 (21:59 -0500)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 7 Feb 2012 02:59:29 +0000 (21:59 -0500)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
tests/test_kernel_data_trace.c
tests/test_sessions.c
tests/test_ust_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)
index f444e30438bb3aeff3a3d9ee54531788b40aeb79..3e876c88990c15cc1dbb14a9a98be1bfea202ceb 100644 (file)
@@ -37,6 +37,7 @@
 #define PATH1 "/tmp/.test-junk-lttng"
 
 #define MAX_SESSIONS 10000
+#define RANDOM_STRING_LEN      11
 
 /*
  * String of 263 caracters. NAME_MAX + "OVERFLOW". If OVERFLOW appears in the
@@ -61,22 +62,23 @@ static const char alphanum[] =
        "0123456789"
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        "abcdefghijklmnopqrstuvwxyz";
+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;
 }
 
 /*
index 6acd7a0d083adba911d171e9c2d69e4fd10ada19..924cfe4b97f1fde75af52f075969280072eab932 100644 (file)
@@ -35,6 +35,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,25 +45,26 @@ static const char alphanum[] =
        "0123456789"
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        "abcdefghijklmnopqrstuvwxyz";
+static char random_string[RANDOM_STRING_LEN];
 
 static struct ltt_ust_session *usess;
 static struct lttng_domain dom;
 
 /*
  * 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_ust_session(void)
This page took 0.029372 seconds and 5 git commands to generate.