kern/104389: [geom] [patch] sys/geom/geom_dump.c doesn't encode XML entities

doug steinwand dzs-pr at dzs.fx.org
Wed Mar 7 15:37:08 UTC 2007


* Dennis Berger <db at nipsi.de> [2007-03-06 16:53:44 +0100]:
> This way we can escape all illegal characters.
> What do you suggest?

The gstat and other geom applications basically use expat to parse
the equivalent of "sysctl -b kern.geom.confxml". This output does
not have an encoding specified, so expat accepts only ASCII.  As
such, bytes greater than 0x7e must be encoded.

http://skew.org/xml/tutorial/
http://www.w3.org/TR/1998/REC-xml-19980210

Attached is a patch which attempts to output valid XML for all cases
(any value between 0x00 and 0xff). One issue is that many bytes
between 0x00 and 0x1f have no valid XML coding, so this patch
replaces them with '?' (such things should not appear in geom names,
though).

Also, it seems that expat is attempting to convert bytes from
iso-8859-1 into utf8 characters, so gstat and glabel output may
look weird.

 - doug
-------------- next part --------------
--- sys/geom/geom_dump.c.orig	Wed Mar 10 00:49:08 2004
+++ sys/geom/geom_dump.c	Tue Mar  6 20:15:20 2007
@@ -45,6 +45,31 @@
 #include <geom/geom.h>
 #include <geom/geom_int.h>
 
+static void
+encode_bytes(struct sbuf *sb, const char *fmt, const char *str, int len)
+{
+	char *d, *dst;
+	unsigned char c;
+	int i;
+
+	if (len == -1)
+		len = strlen(str);
+	/* allocate for worst-case expansion. */
+	dst = d = g_malloc(len * 6 + 1, M_WAITOK); 
+	for(i = 0; i < len; i++) {
+		c = (unsigned char) str[i];
+		if (c == '&' || c == '<' || c == '>' ||
+		    c == '\'' || c == '"' || c > 0x7e)
+			d += sprintf(d, "&#x%X;", c);
+		else if (c == 0x9 || c == 0xa || c == 0xd || c > 0x1f)
+			*d++ = c;
+		else
+			*d++ = '?';
+	}
+	*d = '\0';
+	sbuf_printf(sb, fmt, dst);
+	g_free(dst);
+}
 
 static void
 g_confdot_consumer(struct sbuf *sb, struct g_consumer *cp)
@@ -182,7 +207,7 @@
 	sbuf_printf(sb, "\t  <geom ref=\"%p\"/>\n", pp->geom);
 	sbuf_printf(sb, "\t  <mode>r%dw%de%d</mode>\n",
 	    pp->acr, pp->acw, pp->ace);
-	sbuf_printf(sb, "\t  <name>%s</name>\n", pp->name);
+	encode_bytes(sb, "\t  <name>%s</name>\n", pp->name, -1);
 	sbuf_printf(sb, "\t  <mediasize>%jd</mediasize>\n",
 	    (intmax_t)pp->mediasize);
 	sbuf_printf(sb, "\t  <sectorsize>%u</sectorsize>\n", pp->sectorsize);
@@ -205,7 +230,7 @@
 
 	sbuf_printf(sb, "    <geom id=\"%p\">\n", gp);
 	sbuf_printf(sb, "      <class ref=\"%p\"/>\n", gp->class);
-	sbuf_printf(sb, "      <name>%s</name>\n", gp->name);
+	encode_bytes(sb, "      <name>%s</name>\n", gp->name, -1);
 	sbuf_printf(sb, "      <rank>%d</rank>\n", gp->rank);
 	if (gp->flags & G_GEOM_WITHER)
 		sbuf_printf(sb, "      <wither/>\n");
@@ -234,7 +259,7 @@
 	struct g_geom *gp2;
 
 	sbuf_printf(sb, "  <class id=\"%p\">\n", mp);
-	sbuf_printf(sb, "    <name>%s</name>\n", mp->name);
+	encode_bytes(sb, "    <name>%s</name>\n", mp->name, -1);
 	LIST_FOREACH(gp2, &mp->geom, geom) {
 		if (gp != NULL && gp != gp2)
 			continue;


More information about the freebsd-geom mailing list