configure: add '-Wundef' to warning flags
[lttng-tools.git] / src / common / context.cpp
1 /*
2 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #include "context.h"
9 #include <stddef.h>
10 #include <string.h>
11 #include <common/error.h>
12 #include <common/macros.h>
13
14 int parse_application_context(const char *str, char **out_provider_name,
15 char **out_ctx_name)
16 {
17 const char app_ctx_prefix[] = "$app.";
18 char *provider_name = NULL, *ctx_name = NULL;
19 size_t i, len, colon_pos = 0, provider_name_len, ctx_name_len;
20
21 if (!str || !out_provider_name || !out_ctx_name) {
22 goto not_found;
23 }
24
25 len = strlen(str);
26 if (len <= sizeof(app_ctx_prefix) - 1) {
27 goto not_found;
28 }
29
30 /* String starts with $app. */
31 if (strncmp(str, app_ctx_prefix, sizeof(app_ctx_prefix) - 1)) {
32 goto not_found;
33 }
34
35 /* Validate that the ':' separator is present. */
36 for (i = sizeof(app_ctx_prefix); i < len; i++) {
37 const char c = str[i];
38
39 if (c == ':') {
40 colon_pos = i;
41 break;
42 }
43 }
44
45 /*
46 * No colon found or no ctx name ("$app.provider:") or no provider name
47 * given ("$app.:..."), which is invalid.
48 */
49 if (!colon_pos || colon_pos == len ||
50 colon_pos == sizeof(app_ctx_prefix)) {
51 goto not_found;
52 }
53
54 provider_name_len = colon_pos - sizeof(app_ctx_prefix) + 2;
55 provider_name = (char *) zmalloc(provider_name_len);
56 if (!provider_name) {
57 PERROR("malloc provider_name");
58 goto not_found;
59 }
60 strncpy(provider_name, str + sizeof(app_ctx_prefix) - 1,
61 provider_name_len - 1);
62
63 ctx_name_len = len - colon_pos;
64 ctx_name = (char *) zmalloc(ctx_name_len);
65 if (!ctx_name) {
66 PERROR("malloc ctx_name");
67 goto not_found;
68 }
69 strncpy(ctx_name, str + colon_pos + 1, ctx_name_len - 1);
70
71 *out_provider_name = provider_name;
72 *out_ctx_name = ctx_name;
73 return 0;
74 not_found:
75 free(provider_name);
76 free(ctx_name);
77 return -1;
78 }
79
This page took 0.033183 seconds and 4 git commands to generate.