From 7b4a379271c94575d31ae3d4279de2f87ec01351 Mon Sep 17 00:00:00 2001 From: Sean Bright Date: Mon, 31 Jan 2022 13:52:26 -0500 Subject: [PATCH] manager.c: Generate valid XML if attributes have leading digits. XML element attributes are not permitted to begin with digits, so prepend with an underscore to avoid failing XML validation. This is not backwards compatible but my assumption is that compliant XML parsers would already have been complaining about this. ASTERISK-29886 #close Change-Id: Icfaa56a131a082d803e9b7db5093806d455a0523 --- main/manager.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/main/manager.c b/main/manager.c index 23e5575102..1a1016f4b8 100644 --- a/main/manager.c +++ b/main/manager.c @@ -7644,6 +7644,7 @@ static void xml_copy_escape(struct ast_str **out, const char *src, int mode) /* store in a local buffer to avoid calling ast_str_append too often */ char buf[256]; char *dst = buf; + const char *save = src; int space = sizeof(buf); /* repeat until done and nothing to flush */ for ( ; *src || dst != buf ; src++) { @@ -7657,10 +7658,19 @@ static void xml_copy_escape(struct ast_str **out, const char *src, int mode) } } - if ( (mode & 2) && !isalnum(*src)) { - *dst++ = '_'; - space--; - continue; + if (mode & 2) { + if (save == src && isdigit(*src)) { + /* The first character of an XML attribute cannot be a digit */ + *dst++ = '_'; + *dst++ = *src; + space -= 2; + continue; + } else if (!isalnum(*src)) { + /* Replace non-alphanumeric with an underscore */ + *dst++ = '_'; + space--; + continue; + } } switch (*src) { case '<': -- 2.32.0