[Asterisk-code-review] manager.c: Generate valid XML if attributes have leading digits. (asterisk[16])

Sean Bright asteriskteam at digium.com
Mon Jan 31 12:56:41 CST 2022


Sean Bright has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/17947 )


Change subject: manager.c: Generate valid XML if attributes have leading digits.
......................................................................

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
---
M main/manager.c
1 file changed, 14 insertions(+), 4 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/47/17947/1

diff --git a/main/manager.c b/main/manager.c
index 23e5575..1a1016f 100644
--- a/main/manager.c
+++ b/main/manager.c
@@ -7644,6 +7644,7 @@
 	/* 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 @@
 			}
 		}
 
-		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 '<':

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/17947
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 16
Gerrit-Change-Id: Icfaa56a131a082d803e9b7db5093806d455a0523
Gerrit-Change-Number: 17947
Gerrit-PatchSet: 1
Gerrit-Owner: Sean Bright <sean at seanbright.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20220131/4776648c/attachment.html>


More information about the asterisk-code-review mailing list