Fix: sessiond: use after free
[deliverable/lttng-tools.git] / src / common / context.c
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 LTTNG_HIDDEN
15 int parse_application_context(const char *str, char **out_provider_name,
16 char **out_ctx_name)
17 {
18 const char app_ctx_prefix[] = "$app.";
19 char *provider_name = NULL, *ctx_name = NULL;
20 size_t i, len, colon_pos = 0, provider_name_len, ctx_name_len;
21
22 if (!str || !out_provider_name || !out_ctx_name) {
23 goto not_found;
24 }
25
26 len = strlen(str);
27 if (len <= sizeof(app_ctx_prefix) - 1) {
28 goto not_found;
29 }
30
31 /* String starts with $app. */
32 if (strncmp(str, app_ctx_prefix, sizeof(app_ctx_prefix) - 1)) {
33 goto not_found;
34 }
35
36 /* Validate that the ':' separator is present. */
37 for (i = sizeof(app_ctx_prefix); i < len; i++) {
38 const char c = str[i];
39
40 if (c == ':') {
41 colon_pos = i;
42 break;
43 }
44 }
45
46 /*
47 * No colon found or no ctx name ("$app.provider:") or no provider name
48 * given ("$app.:..."), which is invalid.
49 */
50 if (!colon_pos || colon_pos == len ||
51 colon_pos == sizeof(app_ctx_prefix)) {
52 goto not_found;
53 }
54
55 provider_name_len = colon_pos - sizeof(app_ctx_prefix) + 2;
56 provider_name = zmalloc(provider_name_len);
57 if (!provider_name) {
58 PERROR("malloc provider_name");
59 goto not_found;
60 }
61 strncpy(provider_name, str + sizeof(app_ctx_prefix) - 1,
62 provider_name_len - 1);
63
64 ctx_name_len = len - colon_pos;
65 ctx_name = zmalloc(ctx_name_len);
66 if (!ctx_name) {
67 PERROR("malloc ctx_name");
68 goto not_found;
69 }
70 strncpy(ctx_name, str + colon_pos + 1, ctx_name_len - 1);
71
72 *out_provider_name = provider_name;
73 *out_ctx_name = ctx_name;
74 return 0;
75 not_found:
76 free(provider_name);
77 free(ctx_name);
78 return -1;
79 }
80
This page took 0.033354 seconds and 5 git commands to generate.