1
0
Fork 0
mirror of https://github.com/warmcat/libwebsockets.git synced 2025-03-30 00:00:16 +01:00

ss: take care to free any metadata heap values before overwrite

Break out the core ss_set_metadata action into a subfunction that
takes the lws_ss_metadata_t, and is fixed to retire heap-based
values before they go out of scope, and adapt the exported version
to call through to that.

Simplify extract_metadata() to reuse the subfunction as well, in
both well-known and custom header cases.
This commit is contained in:
Jed Lu 2020-10-07 17:51:58 +01:00 committed by Andy Green
parent 5f7da4a530
commit ee78b90c8c
3 changed files with 40 additions and 15 deletions

View file

@ -55,6 +55,30 @@ lws_ss_policy_lookup(const struct lws_context *context, const char *streamtype)
return NULL; return NULL;
} }
int
_lws_ss_set_metadata(lws_ss_metadata_t *omd, const char *name,
const void *value, size_t len)
{
/*
* If there was already a heap-based value, it's about to go out of
* scope due to us trashing the pointer. So free it first and clear
* its flag indicating it's heap-based.
*/
if (omd->value_on_lws_heap) {
lws_free_set_NULL(omd->value);
omd->value_on_lws_heap = 0;
}
// lwsl_notice("%s: %s %s\n", __func__, name, (const char *)value);
omd->name = name;
omd->value = (void *)value;
omd->length = len;
return 0;
}
int int
lws_ss_set_metadata(struct lws_ss_handle *h, const char *name, lws_ss_set_metadata(struct lws_ss_handle *h, const char *name,
const void *value, size_t len) const void *value, size_t len)
@ -66,13 +90,7 @@ lws_ss_set_metadata(struct lws_ss_handle *h, const char *name,
return 1; return 1;
} }
// lwsl_notice("%s: %s %s\n", __func__, name, (const char *)value); return _lws_ss_set_metadata(omd, name, value, len);
omd->name = name;
omd->value = (void *)value;
omd->length = len;
return 0;
} }
int int

View file

@ -424,6 +424,10 @@ int
lws_ss_exp_cb_metadata(void *priv, const char *name, char *out, size_t *pos, lws_ss_exp_cb_metadata(void *priv, const char *name, char *out, size_t *pos,
size_t olen, size_t *exp_ofs); size_t olen, size_t *exp_ofs);
int
_lws_ss_set_metadata(lws_ss_metadata_t *omd, const char *name,
const void *value, size_t len);
int int
_lws_ss_client_connect(lws_ss_handle_t *h, int is_retry); _lws_ss_client_connect(lws_ss_handle_t *h, int is_retry);

View file

@ -243,23 +243,25 @@ lws_extract_metadata(lws_ss_handle_t *h, struct lws *wsi)
if (n) { if (n) {
const char *cp = lws_hdr_simple_ptr(wsi, const char *cp = lws_hdr_simple_ptr(wsi,
polmd->value_is_http_token); polmd->value_is_http_token);
omd = lws_ss_get_handle_metadata(h, polmd->name);
if (!omd)
return 1;
assert(!strcmp(omd->name, polmd->name));
/* /*
* it's present on the wsi, we want to * it's present on the wsi, we want to
* set the related metadata name to it then * set the related metadata name to it then
*/ */
lws_ss_set_metadata(h, polmd->name, cp, n); _lws_ss_set_metadata(omd, polmd->name, cp, n);
#if defined(LWS_WITH_SECURE_STREAMS_PROXY_API)
/* /*
* ...and because we are doing it from parsing * ...and because we are doing it from parsing
* onward rx, we want to mark the metadata as * onward rx, we want to mark the metadata as
* needing passing to the client * needing passing to the client
*/ */
omd = lws_ss_get_handle_metadata(h, polmd->name);
assert(!strcmp(omd->name, polmd->name));
#if defined(LWS_WITH_SECURE_STREAMS_PROXY_API)
omd->pending_onward = 1; omd->pending_onward = 1;
#endif #endif
} }
@ -309,12 +311,13 @@ lws_extract_metadata(lws_ss_handle_t *h, struct lws *wsi)
omd = lws_ss_get_handle_metadata(h, omd = lws_ss_get_handle_metadata(h,
polmd->name); polmd->name);
_lws_ss_set_metadata(omd, polmd->name,
p, (size_t)n);
omd->value_on_lws_heap = 1;
#if defined(LWS_WITH_SECURE_STREAMS_PROXY_API) #if defined(LWS_WITH_SECURE_STREAMS_PROXY_API)
omd->pending_onward = 1; omd->pending_onward = 1;
#endif #endif
omd->value = p;
omd->length = (size_t)n;
omd->value_on_lws_heap = 1;
} }
} }
#endif #endif