Clean-up: coverity warns of uncaught exception during logging
[lttng-tools.git] / src / common / uuid.cpp
index a820a8a4191defbef75ae63c2085540f8f5dfcae..4a1038b330caf66e4be141bc251e5dcd14b965b6 100644 (file)
@@ -7,6 +7,10 @@
  */
 
 #include <common/compat/string.hpp>
+#include <common/error.hpp>
+#include <common/format.hpp>
+#include <common/random.hpp>
+
 #include <stddef.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -26,6 +30,18 @@ void lttng_uuid_to_str(const lttng_uuid& uuid, char *uuid_str)
        sprintf(uuid_str, LTTNG_UUID_FMT, LTTNG_UUID_FMT_VALUES(uuid));
 }
 
+std::string lttng::utils::uuid_to_str(const lttng_uuid& uuid)
+{
+       std::string uuid_str(LTTNG_UUID_STR_LEN, '\0');
+
+       ::lttng_uuid_to_str(uuid, &uuid_str[0]);
+
+       /* Don't include '\0' in the C++ string. */
+       uuid_str.resize(uuid_str.size() - 1);
+
+       return uuid_str;
+}
+
 int lttng_uuid_from_str(const char *str_in, lttng_uuid& uuid_out)
 {
        int ret = 0;
@@ -45,6 +61,7 @@ int lttng_uuid_from_str(const char *str_in, lttng_uuid& uuid_out)
        if (sscanf(str_in, LTTNG_UUID_FMT, LTTNG_UUID_SCAN_VALUES(uuid_scan)) !=
                        LTTNG_UUID_LEN) {
                ret = -1;
+               goto end;
        }
 
        uuid_out = uuid_scan;
@@ -65,18 +82,15 @@ int lttng_uuid_generate(lttng_uuid& uuid_out)
        int i, ret = 0;
 
        if (!lttng_uuid_is_init) {
-               /*
-                * We don't need cryptographic quality randomness to
-                * generate UUIDs, seed rand with the epoch.
-                */
-               const time_t epoch = time(NULL);
-
-               if (epoch == (time_t) -1) {
+               try {
+                       srand(lttng::random::produce_best_effort_random_seed());
+               } catch (std::exception& e) {
+                       ERR("Failed to initialize random seed during generation of UUID: %s",
+                                       e.what());
                        ret = -1;
                        goto end;
                }
 
-               srand(epoch);
                lttng_uuid_is_init = true;
        }
 
This page took 0.024567 seconds and 4 git commands to generate.