sdp-xml: Use a queue to collect sequence members
Subsystems (2)
Luiz Augusto von Dentz
maintainerPATCH v2 1/4 sdp-xml: Use a queue to collect sequence members
From: Luiz Augusto von Dentz <[email protected]>
Appending a member to a sequence with sdp_seq_append() walks the single-linked list to find its tail, so building a sequence is O(n^2).
This was previously worked around by caching the tail of the sequence in struct sdp_xml_data, which required the caller to pick between appending to the cached tail and initialising val.dataseq, and to keep the cache in sync on every append.
Collect the members in a struct queue instead, which tracks its own tail, and link them into val.dataseq once the element is closed. Appending is a plain queue_push_tail(), and the queue is destroyed along with the rest of the element so members that were never linked, such as on malformed input, are still freed.
The sequence_on_squared() test stays at less than 0.1 seconds.
--- Makefile.tools | 4 +++- src/sdp-xml.c | 60 ++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 46 insertions(+), 18 deletions(-)
Makefile.tools +3 -1
@@ -437,7 +437,9 @@ tools_hciconfig_LDADD = lib/libbluetooth-internal.la tools_hcitool_SOURCES = tools/hcitool.c src/oui.h src/oui.c tools_hcitool_LDADD = lib/libbluetooth-internal.la $(UDEV_LIBS) -tools_sdptool_SOURCES = tools/sdptool.c src/sdp-xml.h src/sdp-xml.c +tools_sdptool_SOURCES = tools/sdptool.c src/sdp-xml.h src/sdp-xml.c \ + src/shared/queue.h src/shared/queue.c \ + src/shared/util.h src/shared/util.c tools_sdptool_LDADD = lib/libbluetooth-internal.la $(GLIB_LIBS) tools_ciptool_LDADD = lib/libbluetooth-internal.la
src/sdp-xml.c +43 -17
98 lines not on this page · raw message
Signature · 1 line
2.54.0
Luiz Augusto von Dentz
maintainerPATCH v2 2/4 sdp-xml: Fix leaking the parse stack on malformed input
From: Luiz Augusto von Dentz <[email protected]>
sdp_xml_parse_record() frees its context but never the elements left on ctx_data->stack_head.
element_end() returns early without popping the stack when it rejects a document, for instance on a mismatched </sequence> close, so a malformed record leaves its elements behind and they are never freed.
Free the remaining stack elements before returning. Found with the compute-seq-size-type-confusion.xml test:
56 (direct) + 1,072 (indirect) bytes in 1 blocks are definitely lost
at calloc (vg_replace_malloc.c:1678)
by sdp_xml_data_alloc (sdp-xml.c:73)
by element_start (sdp-xml.c:473)
by g_markup_parse_context_parse (gmarkup.c:1369)
by sdp_xml_parse_record (sdp-xml.c:696)
--- src/sdp-xml.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
src/sdp-xml.c +14 -0
@@ -551,6 +551,17 @@ static void sdp_xml_data_free(struct sdp_xml_data *elem)free (elem); } +/* Free the elements left on the stack, e.g. by a document that is malformed */ +static void sdp_xml_data_free_stack (struct sdp_xml_data *elem) +{ +while (elem) { +struct sdp_xml_data *next = elem->next; + +sdp_xml_data_free (elem); + elem = next; + } +} +static void element_end (GMarkupParseContext *context,const char *element_name,gpointer user_data,GError **err) {
@@ -696,6 +707,7 @@ sdp_record_t *sdp_xml_parse_record(const char *data, int size)if (g_markup_parse_context_parse (ctx, data, size,NULL ) == FALSE) {error ("XML parsing error" );g_markup_parse_context_free (ctx); +sdp_xml_data_free_stack (ctx_data->stack_head);sdp_record_free (record);free (ctx_data);return NULL ;
@@ -703,6 +715,8 @@ sdp_record_t *sdp_xml_parse_record(const char *data, int size)g_markup_parse_context_free (ctx); +sdp_xml_data_free_stack (ctx_data->stack_head); +free (ctx_data);return record;
Signature · 1 line
2.54.0
Luiz Augusto von Dentz
maintainerPATCH v2 3/4 sdp: Fix memory leak when freeing alternates
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
sdp_data_alloc_with_length() stores the members of SDP_ALT8, SDP_ALT16 and SDP_ALT32 in val.dataseq, exactly like it does for the SDP_SEQ8, SDP_SEQ16 and SDP_SEQ32 sequences.
sdp_data_free() only calls data_seq_free() for the sequences though, so freeing an alternate frees the alternate itself and leaks every one of its members, along with anything they own in turn:
209 (48 direct, 161 indirect) bytes in 1 blocks are definitely lost
at calloc (vg_replace_malloc.c:1678)
by sdp_data_alloc_with_length (sdp.c:350)
by sdp_data_alloc (sdp.c:486)
by sdp_xml_parse_int (sdp-xml.c:243)
by sdp_xml_parse_datatype (sdp-xml.c:421)
by element_start (sdp-xml.c:507)
Free the members of alternates as well.
--- lib/bluetooth/sdp.c | 3 +++ 1 file changed, 3 insertions(+)
lib/bluetooth/sdp.c +3 -0
@@ -972,6 +972,9 @@ void sdp_data_free(sdp_data_t *d)case SDP_SEQ8 :case SDP_SEQ16 :case SDP_SEQ32 : +case SDP_ALT8 : +case SDP_ALT16 : +case SDP_ALT32 :data_seq_free (d);break ;case SDP_URL_STR8 :
Signature · 1 line
2.54.0
Luiz Augusto von Dentz
maintainerPATCH v2 4/4 unit/test-sdp-xml: Add a test parsing alternates
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
None of the existing records contain an <alternate>, so nothing covered the SDP_ALT8, SDP_ALT16 and SDP_ALT32 handling.
Add a record with an alternate holding an integer, a string and a nested sequence, which leaks its members under valgrind without the previous sdp_data_free() fix.
--- Makefile.am | 1 + unit/sdp-xml/alternate.xml | 12 ++++++++++++ unit/test-sdp-xml.c | 1 + 3 files changed, 14 insertions(+) create mode 100644 unit/sdp-xml/alternate.xml
Makefile.am +1 -0
@@ -648,6 +648,7 @@ unit_test_sdp_xml_CFLAGS = $(AM_CFLAGS) $(GLIB_CFLAGS) -DTOP_SRCDIR=\""$(srcdir) unit_test_sdp_xml_CPPFLAGS = -I$(srcdir)/lib EXTRA_DIST += unit/sdp-xml/Bluetooth_HID-sdp_record.xml \ + unit/sdp-xml/alternate.xml \ unit/sdp-xml/compute-seq-size-type-confusion.xml \ unit/sdp-xml/duplicate-attribute.xml \ unit/sdp-xml/qt-SerialPortSDPRecord.xml
unit/sdp-xml/alternate.xml new file +12 -0
@@ -0,0 +1,12 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<record> + <attribute id="0x0004"> + <alternate> + <uint32 value="0x11223344" /> + <text value="alternate-member" /> + <sequence> + <uint16 value="0x0100" /> + </sequence> + </alternate> + </attribute> +</record>
unit/test-sdp-xml.c +1 -0
@@ -133,6 +133,7 @@ int main(int argc, char *argv[])DEFINE_TEST ("compute-seq-size-type-confusion.xml" , FALSE);/* From https://github.com/bluez/bluez/security/advisories/GHSA-75v6-6q44-57hc */ DEFINE_TEST ("duplicate-attribute.xml" , TRUE); +DEFINE_TEST ("alternate.xml" , TRUE); tester_add("/sequence_on_squared", &data, sequence_on_squared_setup,
Signature · 1 line
2.54.0
This is automated email and please do not reply to this email!
Dear submitter,
Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=1147412
---Test result---
Test Summary:
CheckPatch FAIL 1.43 seconds GitLint PASS 0.96 seconds BuildEll PASS 20.76 seconds BluezMake PASS 611.79 seconds MakeCheck PASS 19.04 seconds MakeDistcheck PASS 159.66 seconds CheckValgrind PASS 231.94 seconds CheckSmatch PASS 315.81 seconds bluezmakeextell PASS 103.59 seconds IncrementalBuild PASS 645.38 seconds ScanBuild PASS 991.07 seconds
Details
##############################
Test: CheckPatch - FAIL
Desc: Run checkpatch.pl script
Output:
[BlueZ,v2,1/4] sdp-xml: Use a queue to collect sequence members WARNING:BAD_SIGN_OFF: Non-standard signature: Assisted-by:
#113:
ERROR:BAD_SIGN_OFF: Unrecognized email address: 'Claude:claude-opus-5' #113:
/github/workspace/src/patch/14754210.patch total: 1 errors, 1 warnings, 108 lines checked
NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.
/github/workspace/src/patch/14754210.patch has style problems, please review.
NOTE: Ignored message types: COMMIT_MESSAGE COMPLEX_MACRO CONST_STRUCT FILE_PATH_CHANGES MISSING_SIGN_OFF PREFER_PACKED SPDX_LICENSE_TAG SPLIT_STRING SSCANF_TO_KSTRTO
NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.
[BlueZ,v2,2/4] sdp-xml: Fix leaking the parse stack on malformed input WARNING:BAD_SIGN_OFF: Non-standard signature: Assisted-by:
#119:
ERROR:BAD_SIGN_OFF: Unrecognized email address: 'Claude:claude-opus-5' #119:
/github/workspace/src/patch/14754211.patch total: 1 errors, 1 warnings, 32 lines checked
NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.
/github/workspace/src/patch/14754211.patch has style problems, please review.
NOTE: Ignored message types: COMMIT_MESSAGE COMPLEX_MACRO CONST_STRUCT FILE_PATH_CHANGES MISSING_SIGN_OFF PREFER_PACKED SPDX_LICENSE_TAG SPLIT_STRING SSCANF_TO_KSTRTO
NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.
[BlueZ,v2,3/4] sdp: Fix memory leak when freeing alternates WARNING:BAD_SIGN_OFF: Non-standard signature: Assisted-by:
#119:
ERROR:BAD_SIGN_OFF: Unrecognized email address: 'Claude:claude-opus-5' #119:
/github/workspace/src/patch/14754212.patch total: 1 errors, 1 warnings, 9 lines checked
NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.
/github/workspace/src/patch/14754212.patch has style problems, please review.
NOTE: Ignored message types: COMMIT_MESSAGE COMPLEX_MACRO CONST_STRUCT FILE_PATH_CHANGES MISSING_SIGN_OFF PREFER_PACKED SPDX_LICENSE_TAG SPLIT_STRING SSCANF_TO_KSTRTO
NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.
[BlueZ,v2,4/4] unit/test-sdp-xml: Add a test parsing alternates WARNING:BAD_SIGN_OFF: Non-standard signature: Assisted-by:
#108:
ERROR:BAD_SIGN_OFF: Unrecognized email address: 'Claude:claude-opus-5' #108:
/github/workspace/src/patch/14754213.patch total: 1 errors, 1 warnings, 26 lines checked
NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.
/github/workspace/src/patch/14754213.patch has style problems, please review.
NOTE: Ignored message types: COMMIT_MESSAGE COMPLEX_MACRO CONST_STRUCT FILE_PATH_CHANGES MISSING_SIGN_OFF PREFER_PACKED SPDX_LICENSE_TAG SPLIT_STRING SSCANF_TO_KSTRTO
NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.
https://github.com/bluez/bluez/pull/2410
---
Regards,
Linux Bluetooth
Patchset looks good to me, thanks.
Quoted 179 lines · Luiz Augusto von Dentz
From: Luiz Augusto von Dentz <[email protected]>
Appending a member to a sequence with
sdp_seq_append()walks the single-linked list to find its tail, so building a sequence is O(n^2).This was previously worked around by caching the tail of the sequence in
structsdp_xml_data, which required the caller to pick between appending
to the cached tail and initialising val.dataseq, and to keep the cache in
sync on every append.Collect the members in a struct queue instead, which tracks its own tail,
and link them into val.dataseq once the element is closed. Appending is a
plainqueue_push_tail(), and the queue is destroyed along with the rest
of the element so members that were never linked, such as on malformed
input, are still freed.The
sequence_on_squared()test stays at less than 0.1 seconds.Assisted-by Claude:claude-opus-5---
Makefile.tools | 4 +++-
src/sdp-xml.c | 60 ++++++++++++++++++++++++++++++++++++------------ --2 files changed, 46 insertions(+), 18 deletions(-)Makefile.tools +0
-0@@ -437,7 +437,9 @@ tools_hciconfig_LDADD = lib/libbluetooth-internal.la
tools_hcitool_SOURCES =tools/hcitool.csrc/oui.hsrc/oui.ctools_hcitool_LDADD=lib/libbluetooth-internal.la$(UDEV_LIBS)Makefile.tools +1
-1-tools_sdptool_SOURCES = tools/sdptool.c src/sdp-xml.h src/sdp-xml.c +tools_sdptool_SOURCES = tools/sdptool.c src/sdp-xml.h src/sdp-xml.c\
Makefile.tools +2
-0+ src/shared/queue.h src/shared/queue.c \ + src/shared/util.h src/shared/util.ctools_sdptool_LDADD =
lib/libbluetooth-internal.la$(GLIB_LIBS)
tools_ciptool_LDADD =lib/libbluetooth-internal.lasrc/sdp-xml.c +0
-0@@ -25,6 +25,8 @@ #include "bluetooth/sdp.h" #include "bluetooth/sdp_lib.h"src/sdp-xml.c +2
-0+#include "shared/queue.h" +#include "
sdp-xml.h"
#define DBG(...) (void)(0)src/sdp-xml.c +0
-0@@ -44,7 +46,7 @@ struct sdp_xml_data {char type; /* 0 = Text or Hexadecimal
*/
char *name; /* Name, optional in the dtd
*/
/* TODO: What is it used for? */src/sdp-xml.c +0
-1-sdp_data_t *tail;/* Tail for O(1) dataseq append */
src/sdp-xml.c +1
-0+struct queue *seq;/* Members of a dataseq, if any */
};
structcontext_data{src/sdp-xml.c +0
-0@@ -510,8 +512,37 @@ static void element_start(GMarkupParseContext *context, } }
+/*
+ * Link the members collected inelem->seqinto elem->data-Quoted 1 line
val.dataseq.
+ *
+ * Members are collected in a queue so that appending is O(1),sdp_seq_append()
+ * would otherwise have to walk to the tail of the sequence on every append.
+ */src/sdp-xml.c +9
-0+static void sdp_xml_data_flush_seq(struct sdp_xml_data *elem) +{ + const struct queue_entry *entry; + sdp_data_t *tail = NULL; + + if (!elem->seq) + return; + + for (entry = queue_get_entries(elem->seq); entry; entry =
entry->next) {src/sdp-xml.c +4
-0+if (tail) +sdp_seq_append (tail, entry->data); +else + elem->data->val.dataseq =sdp_seq_append(NULL,
src/sdp-xml.c +1
-0+ entr
y->data);src/sdp-xml.c +7
-0+ tail = entry->data; + } + +queue_destroy (elem->seq,NULL ); + elem->seq =NULL ; +} +static void
sdp_xml_data_free(struct sdp_xml_data *elem){src/sdp-xml.c +1
-0+ queue_destroy(elem->seq, (queue_destroy_func_t)
sdp_data_free);
+
if (elem->data)
sdp_data_free(elem->data);src/sdp-xml.c +0
-0@@ -568,6 +599,8 @@ static void element_end(GMarkupParseContext *context, return; }src/sdp-xml.c +2
-0+sdp_xml_data_flush_seq (ctx_data->stack_head); +if (!strcmp(element_name, "sequence")) {
if (!SDP_IS_SEQ(ctx_data->stack_head->data->dtd)) { g_set_error(err,G_MARKUP_ERROR,src/sdp-xml.c +0
-0@@ -610,28 +643,21 @@ static void element_end(GMarkupParseContext *context,
if (ctx_data->stack_head->next&&ctx_data->stack_head->data&&
ctx_data->stack_head->next-Quoted 1 line
data) {
src/sdp-xml.c +1
-2-sdp_data_t *tail; -switch (ctx_data->stack_head->next->data->dtd) { + struct sdp_xml_data *parent = ctx_data->stack_head-Quoted 1 line
next;
src/sdp-xml.c +2
-0+ +switch (parent->data->dtd) {case
SDP_SEQ8:
caseSDP_SEQ16:
caseSDP_SEQ32:
caseSDP_ALT8:
caseSDP_ALT16:
caseSDP_ALT32:src/sdp-xml.c +0
-1- tail = ctx_data->stack_head->next->data-Quoted 1 line
val.dataseq ?
src/sdp-xml.c +0
-1- ctx_data->stack_head->next->tail :NULL;
src/sdp-xml.c +0
-5-if (tail) { -sdp_seq_append (tail, - ctx_data->stack_head->data); - }else { - ctx_data->stack_head->next->data-Quoted 1 line
val.dataseq =
src/sdp-xml.c +0
-2- sdp_seq_append(NULL, - ctx_data-Quoted 1 line
stack_head->data);src/sdp-xml.c +5
-5- } - ctx_data->stack_head->next->tail = - ctx_data->stack_head->data; - ctx_data->stack_head->data =NULL ; - ctx_data->stack_head->tail =NULL ; +if (!parent->seq) + parent->seq =queue_new (); + + if (queue_push_tail(parent->seq, + ctx_data-Quoted 1 line
stack_head->data))src/sdp-xml.c +1
-0+ ctx_data->stack_head->data =NULL ;break;
}
This series was applied to bluetooth/bluez.git (master) by Luiz Augusto von Dentz <[email protected]>:
Hello:
This series was applied to bluetooth/bluez.git (master) by Luiz Augusto von Dentz <[email protected]>:
Quoted 11 lines · On Mon, 17 Aug 2026 17:00:35 -0400 you wrote:
From: Luiz Augusto von Dentz <[email protected]>
Appending a member to a sequence with
sdp_seq_append()walks the single-linked list to find its tail, so building a sequence is O(n^2).This was previously worked around by caching the tail of the sequence in struct
sdp_xml_data, which required the caller to pick between appending to the cached tail and initialising val.dataseq, and to keep the cache in sync on every append.[...]
Here is the summary with links:
- [BlueZ,v2,1/4] sdp-xml: Use a queue to collect sequence members
https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=b21edc49b2c3 - [BlueZ,v2,2/4] sdp-xml: Fix leaking the parse stack on malformed input https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=97521ab4dd79 - [BlueZ,v2,3/4] sdp: Fix memory leak when freeing alternates https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=078ef10a4531 - [BlueZ,v2,4/4] unit/test-sdp-xml: Add a test parsing alternates https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=5abc0045b84a
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html