Skip to content

Commit d211c4b

Browse files
committed
lwsac_assert_valid
1 parent 088972b commit d211c4b

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

include/libwebsockets/lws-lwsac.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,4 +287,25 @@ lwsac_total_overhead(struct lwsac *head);
287287
LWS_VISIBLE LWS_EXTERN uint8_t *
288288
lwsac_scan_extant(struct lwsac *head, uint8_t *find, size_t len, int nul);
289289

290+
/**
291+
* lwsac_assert_valid() - checks if check..check + len is a valid pointer into memory owned by ac
292+
*
293+
* \param ac: the lwsac to confirm with
294+
* \param check: the pointer to check
295+
* \param len: length in bytes of check area
296+
*
297+
* Returns 0 if the pointer is in the lwsac, or nonzero if the pointer is
298+
* not in a valid region of the lwsac
299+
*/
300+
301+
LWS_VISIBLE LWS_EXTERN int
302+
_lwsac_assert_valid(struct lwsac *aco, void *check, size_t len, const char *name_ac, const char *name_blob, const char *filename, int line);
303+
304+
#define __lws_sify(_s) _lws_sify(_s)
305+
#define _lws_sify(_s) #_s
306+
307+
/* use the below macro to invoke the above function */
308+
309+
#define lwsac_assert_valid(_ac, _check, _len) _lwsac_assert_valid(_ac, _check, _len, __lws_sify(_ac), __lws_sify(_check), __FILE__, __LINE__)
310+
290311
///@}

lib/misc/lwsac/lwsac.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* libwebsockets - small server side websockets and web server implementation
33
*
4-
* Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
4+
* Copyright (C) 2010 - 2025 Andy Green <andy@warmcat.com>
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to
@@ -356,3 +356,37 @@ lwsac_detach(struct lwsac **head)
356356
lwsl_debug("%s: head %p: refcount %d: Marked as detached\n",
357357
__func__, *head, lachead->refcount);
358358
}
359+
360+
int
361+
_lwsac_assert_valid(struct lwsac *aco, void *check, size_t len, const char *name_ac, const char *name_blob,
362+
const char *filename, int line)
363+
{
364+
struct lwsac *ac = aco;
365+
366+
while (ac) {
367+
void *pos = (uint8_t *)&ac[1],
368+
*end = ((uint8_t *)ac) + ac->ofs;
369+
370+
if (check >= pos && (void *)(((uint8_t *)check) + len) <= end)
371+
return 0;
372+
373+
ac = ac->next;
374+
}
375+
376+
ac = aco;
377+
378+
while (ac) {
379+
void *pos = (uint8_t *)&ac[1],
380+
*end = ((uint8_t *)ac) + ac->ofs;
381+
382+
lwsl_notice("%s: ac chunk %p -> %p\n", __func__, pos, end);
383+
384+
ac = ac->next;
385+
}
386+
387+
lwsl_err("%s:%d: %s (%p) len %u is not found inside lwsac %s (%p)\n", filename, line, name_blob, check, (unsigned int)len, name_ac, aco);
388+
assert(0); /* checked ptr is not in ac */
389+
390+
return 1;
391+
}
392+

0 commit comments

Comments
 (0)