Fix: liblttng-ctl comm: lttng_event_context is not packed
[deliverable/lttng-tools.git] / src / lib / lttng-ctl / lttng-ctl.c
index 6b745d0204eb30d1c6517c72d2a6314fd1787858..a387202a66e1d192cfa7b586553164ab01d94d8c 100644 (file)
@@ -804,9 +804,10 @@ int lttng_add_context(struct lttng_handle *handle,
                const char *channel_name)
 {
        int ret;
-       size_t len = 0;
-       char *buf = NULL;
        struct lttcomm_session_msg lsm;
+       struct lttng_dynamic_buffer buffer;
+
+       lttng_dynamic_buffer_init(&buffer);
 
        /* Safety check. Both are mandatory. */
        if (handle == NULL || ctx == NULL) {
@@ -833,55 +834,18 @@ int lttng_add_context(struct lttng_handle *handle,
                goto end;
        }
 
-       if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
-               size_t provider_len, ctx_len;
-               const char *provider_name = ctx->u.app_ctx.provider_name;
-               const char *ctx_name = ctx->u.app_ctx.ctx_name;
-
-               if (!provider_name || !ctx_name) {
-                       ret = -LTTNG_ERR_INVALID;
-                       goto end;
-               }
-
-               provider_len = strlen(provider_name);
-               if (provider_len == 0) {
-                       ret = -LTTNG_ERR_INVALID;
-                       goto end;
-               }
-               lsm.u.context.provider_name_len = provider_len;
-
-               ctx_len = strlen(ctx_name);
-               if (ctx_len == 0) {
-                       ret = -LTTNG_ERR_INVALID;
-                       goto end;
-               }
-               lsm.u.context.context_name_len = ctx_len;
-
-               len = provider_len + ctx_len;
-               buf = zmalloc(len);
-               if (!buf) {
-                       ret = -LTTNG_ERR_NOMEM;
-                       goto end;
-               }
-
-               memcpy(buf, provider_name, provider_len);
-               memcpy(buf + provider_len, ctx_name, ctx_len);
+       ret = lttng_event_context_serialize(ctx, &buffer);
+       if (ret) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
        }
-       memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context));
 
-       if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
-               /*
-                * Don't leak application addresses to the sessiond.
-                * This is only necessary when ctx is for an app ctx otherwise
-                * the values inside the union (type & config) are overwritten.
-                */
-               lsm.u.context.ctx.u.app_ctx.provider_name = NULL;
-               lsm.u.context.ctx.u.app_ctx.ctx_name = NULL;
-       }
+       lsm.u.context.length = buffer.size;
 
-       ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, buf, len, NULL);
+       ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(
+                       &lsm, buffer.data, buffer.size, NULL);
 end:
-       free(buf);
+       lttng_dynamic_buffer_reset(&buffer);
        return ret;
 }
 
This page took 0.040216 seconds and 5 git commands to generate.