Build fix: warning: clang warns that uuid_scan may be uninitialized
[lttng-tools.git] / src / common / uuid.cpp
CommitLineData
c70636a7
MJ
1/*
2 * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
4 *
ab5be9fa 5 * SPDX-License-Identifier: LGPL-2.1-only
c70636a7 6 *
c70636a7
MJ
7 */
8
28ab034a
JG
9#include "uuid.hpp"
10
c9e313bc 11#include <common/compat/string.hpp>
57b90af7
JG
12#include <common/error.hpp>
13#include <common/format.hpp>
14#include <common/random.hpp>
15
c70636a7
MJ
16#include <stddef.h>
17#include <stdint.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <time.h>
22
328c2fe7
JG
23namespace {
24const lttng_uuid nil_uuid = {};
25bool lttng_uuid_is_init;
26} /* namespace */
c70636a7 27
328c2fe7 28void lttng_uuid_to_str(const lttng_uuid& uuid, char *uuid_str)
c70636a7
MJ
29{
30 sprintf(uuid_str, LTTNG_UUID_FMT, LTTNG_UUID_FMT_VALUES(uuid));
31}
32
23083fa0
JG
33std::string lttng::utils::uuid_to_str(const lttng_uuid& uuid)
34{
35 std::string uuid_str(LTTNG_UUID_STR_LEN, '\0');
36
37 ::lttng_uuid_to_str(uuid, &uuid_str[0]);
38
39 /* Don't include '\0' in the C++ string. */
40 uuid_str.resize(uuid_str.size() - 1);
41
42 return uuid_str;
43}
44
328c2fe7 45int lttng_uuid_from_str(const char *str_in, lttng_uuid& uuid_out)
c70636a7
MJ
46{
47 int ret = 0;
00c2aa6f 48 lttng_uuid uuid_scan = {};
c70636a7 49
328c2fe7 50 if (str_in == nullptr) {
c70636a7
MJ
51 ret = -1;
52 goto end;
53 }
54
a1298db6 55 if (lttng_strnlen(str_in, LTTNG_UUID_STR_LEN) != LTTNG_UUID_STR_LEN - 1) {
c70636a7
MJ
56 ret = -1;
57 goto end;
58 }
59
60 /* Scan to a temporary location in case of a partial match. */
28ab034a 61 if (sscanf(str_in, LTTNG_UUID_FMT, LTTNG_UUID_SCAN_VALUES(uuid_scan)) != LTTNG_UUID_LEN) {
c70636a7 62 ret = -1;
52cb8c0f 63 goto end;
c70636a7
MJ
64 }
65
328c2fe7 66 uuid_out = uuid_scan;
c70636a7
MJ
67end:
68 return ret;
69}
70
328c2fe7 71bool lttng_uuid_is_nil(const lttng_uuid& uuid)
c70636a7 72{
328c2fe7 73 return uuid == nil_uuid;
c70636a7
MJ
74}
75
76/*
77 * Generate a random UUID according to RFC4122, section 4.4.
78 */
328c2fe7 79int lttng_uuid_generate(lttng_uuid& uuid_out)
c70636a7
MJ
80{
81 int i, ret = 0;
82
c70636a7 83 if (!lttng_uuid_is_init) {
57b90af7
JG
84 try {
85 srand(lttng::random::produce_best_effort_random_seed());
86 } catch (std::exception& e) {
88277a52 87 ERR("Failed to initialize random seed during generation of UUID: %s",
28ab034a 88 e.what());
c70636a7
MJ
89 ret = -1;
90 goto end;
91 }
c70636a7
MJ
92
93 lttng_uuid_is_init = true;
94 }
95
96 /*
97 * Generate 16 bytes of random bits.
98 */
99 for (i = 0; i < LTTNG_UUID_LEN; i++) {
100 uuid_out[i] = (uint8_t) rand();
101 }
102
103 /*
104 * Set the two most significant bits (bits 6 and 7) of the
105 * clock_seq_hi_and_reserved to zero and one, respectively.
106 */
107 uuid_out[8] &= ~(1 << 6);
108 uuid_out[8] |= (1 << 7);
109
110 /*
111 * Set the four most significant bits (bits 12 through 15) of the
112 * time_hi_and_version field to the 4-bit version number from
113 * Section 4.1.3.
114 */
115 uuid_out[6] &= 0x0f;
116 uuid_out[6] |= (LTTNG_UUID_VER << 4);
117
118end:
119 return ret;
120}
This page took 0.050482 seconds and 4 git commands to generate.