kern/104389: sys/geom/geom_dump.c doesn't encode XML entities
douglas steinwand
dzs-pr at dzs.fx.org
Sun Oct 15 13:30:25 PDT 2006
The following reply was made to PR kern/104389; it has been noted by GNATS.
From: douglas steinwand <dzs-pr at dzs.fx.org>
To: Poul-Henning Kamp <phk at phk.freebsd.dk>
Cc: douglas steinwand <dzs-pr at dzs.fx.org>, freebsd-gnats-submit at FreeBSD.org
Subject: Re: kern/104389: sys/geom/geom_dump.c doesn't encode XML entities
Date: Sun, 15 Oct 2006 13:20:41 -0700
--BXVAT5kNtrzKuDFl
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
* Poul-Henning Kamp <phk at phk.freebsd.dk> [2006-10-15 18:12:56 +0000]:
> I'm not keep on the encoding, and would be inclined to say
> "Don't use such names then", but I can probably be convinced
> that this is actually a good idea if sensible examples are shown.
I encountered this issue thanks to a CD from Sun Microsystems:
# glabel status
Name Status Components
iso9660/tools_&_driver_1.0 N/A acd0
The label on its ISO9660 filesystem has "&", which must be encoded
in XML.
> The other thing is that in the patch, the _encode_entities()
> function should not have a leading underscore and should
> take arguments:
>
> static void
> encode_entities(struct sbuf *sb, const char *fmt,
> const char *str, int len);
My initial patch was just a quick hack to work around the problem.
I didn't know how FreeBSD's developers wanted to address it. For
example, if XML will be used widely in the kernel, should a new
format code for kvprintf() do this XML encoding?
> So that usage would not need a randomsized local buffer and
> double enveloping of the call:
>
> >- sbuf_printf(sb, "\t <name>%s</name>\n", pp->name);
>
> Should be:
>
> encode_entities(sb, "\t <name>%s</name>\n", pp->name, -1);
>
> (-1 for len means "use strlen")
Attached is a new patch for geom_dump.c which follows your
recommendations.
Thanks,
- doug
--BXVAT5kNtrzKuDFl
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename=patch-geom_dump
--- sys/geom/geom_dump.c.orig Wed Mar 10 00:49:08 2004
+++ sys/geom/geom_dump.c Sun Oct 15 12:53:42 2006
@@ -45,6 +45,47 @@
#include <geom/geom.h>
#include <geom/geom_int.h>
+static void
+encode_entities(struct sbuf *sb, const char *fmt, const char *str, int len)
+{
+ char *d, *dst;
+ const char *e;
+ int i;
+
+ if (len == -1)
+ len = strlen(str);
+ /* Assume worst case expansion. */
+ dst = d = g_malloc(len * 6 + 1, M_WAITOK);
+
+ for(i = 0; i < len; i++) {
+ switch(str[i]) {
+ case '&':
+ e = "&";
+ break;
+ case '<':
+ e = "<";
+ break;
+ case '>':
+ e = ">";
+ break;
+ case '\'':
+ e = "'";
+ break;
+ case '"':
+ e = """;
+ break;
+ default:
+ *d++ = str[i];
+ continue;
+ }
+ while(*e != '\0')
+ *d++ = *e++;
+ }
+ *d = '\0';
+
+ sbuf_printf(sb, fmt, dst);
+ g_free(dst);
+}
static void
g_confdot_consumer(struct sbuf *sb, struct g_consumer *cp)
@@ -182,7 +223,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_entities(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 +246,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_entities(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 +275,7 @@
struct g_geom *gp2;
sbuf_printf(sb, " <class id=\"%p\">\n", mp);
- sbuf_printf(sb, " <name>%s</name>\n", mp->name);
+ encode_entities(sb, " <name>%s</name>\n", mp->name, -1);
LIST_FOREACH(gp2, &mp->geom, geom) {
if (gp != NULL && gp != gp2)
continue;
--BXVAT5kNtrzKuDFl--
More information about the freebsd-geom
mailing list