Message ID | 1434203538-8075-3-git-send-email-lersek@redhat.com |
---|---|
State | New |
Headers | show |
Laszlo Ersek <lersek@redhat.com> writes: > Cc: Markus Armbruster <armbru@redhat.com> > Cc: Marcel Apfelbaum <marcel@redhat.com> > Cc: Michael S. Tsirkin <mst@redhat.com> > Signed-off-by: Laszlo Ersek <lersek@redhat.com> > --- > > Notes: > v4: > - unchanged > > v3: > - new in v3 > > hw/core/sysbus.c | 16 ++++++---------- > 1 file changed, 6 insertions(+), 10 deletions(-) > > diff --git a/hw/core/sysbus.c b/hw/core/sysbus.c > index b53c351..0ebb4e2 100644 > --- a/hw/core/sysbus.c > +++ b/hw/core/sysbus.c > @@ -281,19 +281,15 @@ static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent) > static char *sysbus_get_fw_dev_path(DeviceState *dev) > { > SysBusDevice *s = SYS_BUS_DEVICE(dev); > - char path[40]; > - int off; > - > - off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev)); > > if (s->num_mmio) { > - snprintf(path + off, sizeof(path) - off, "@"TARGET_FMT_plx, > - s->mmio[0].addr); > - } else if (s->num_pio) { > - snprintf(path + off, sizeof(path) - off, "@i%04x", s->pio[0]); > + return g_strdup_printf("%s@"TARGET_FMT_plx, qdev_fw_name(dev), I'd put a space between "%s@" and TARGET_FMT_plx. > + s->mmio[0].addr); > } > - > - return g_strdup(path); > + if (s->num_pio) { > + return g_strdup_printf("%s@i%04x", qdev_fw_name(dev), s->pio[0]); > + } > + return g_strdup(qdev_fw_name(dev)); > } > > void sysbus_add_io(SysBusDevice *dev, hwaddr addr, Would be nice if we didn't have to triplicate qdev_fw_name(), but I don't have better ideas. Bonus: no arbitrary length limit. Before your patch, it's 39 characters, and the code breaks catastrophically when qdev_fw_name() is longer: the second snprintf() is called with its first argument pointing beyond path[], and its second argument underflowing to a huge size. Textbook example of how not to use snprintf(). Worth mentioning in the commit message? Reviewed-by: Markus Armbruster <armbru@redhat.com>
On 06/15/15 16:23, Markus Armbruster wrote: > Laszlo Ersek <lersek@redhat.com> writes: > >> Cc: Markus Armbruster <armbru@redhat.com> >> Cc: Marcel Apfelbaum <marcel@redhat.com> >> Cc: Michael S. Tsirkin <mst@redhat.com> >> Signed-off-by: Laszlo Ersek <lersek@redhat.com> >> --- >> >> Notes: >> v4: >> - unchanged >> >> v3: >> - new in v3 >> >> hw/core/sysbus.c | 16 ++++++---------- >> 1 file changed, 6 insertions(+), 10 deletions(-) >> >> diff --git a/hw/core/sysbus.c b/hw/core/sysbus.c >> index b53c351..0ebb4e2 100644 >> --- a/hw/core/sysbus.c >> +++ b/hw/core/sysbus.c >> @@ -281,19 +281,15 @@ static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent) >> static char *sysbus_get_fw_dev_path(DeviceState *dev) >> { >> SysBusDevice *s = SYS_BUS_DEVICE(dev); >> - char path[40]; >> - int off; >> - >> - off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev)); >> >> if (s->num_mmio) { >> - snprintf(path + off, sizeof(path) - off, "@"TARGET_FMT_plx, >> - s->mmio[0].addr); >> - } else if (s->num_pio) { >> - snprintf(path + off, sizeof(path) - off, "@i%04x", s->pio[0]); >> + return g_strdup_printf("%s@"TARGET_FMT_plx, qdev_fw_name(dev), > > I'd put a space between "%s@" and TARGET_FMT_plx. I tried to copy the original format string very carefully, but you do have a point. I'll do it in the next version. > >> + s->mmio[0].addr); >> } >> - >> - return g_strdup(path); >> + if (s->num_pio) { >> + return g_strdup_printf("%s@i%04x", qdev_fw_name(dev), s->pio[0]); >> + } >> + return g_strdup(qdev_fw_name(dev)); >> } >> >> void sysbus_add_io(SysBusDevice *dev, hwaddr addr, > > Would be nice if we didn't have to triplicate qdev_fw_name(), but I > don't have better ideas. Right, I could "cache it" in a local variable, but then I'd have to triplicate the variable reference. And, runtime-wise, qdev_fw_name() is called only once, it's just spelled out thrice in the source. > > Bonus: no arbitrary length limit. Before your patch, it's 39 > characters, and the code breaks catastrophically when qdev_fw_name() is > longer: the second snprintf() is called with its first argument pointing > beyond path[], and its second argument underflowing to a huge size. > Textbook example of how not to use snprintf(). Worth mentioning in the > commit message? I was afraid that bashing on old code would only earn me some ire (not from you, mind you -- in general), so I didn't mention this fact in the commit message on purpose. :) I will, in the next version. > Reviewed-by: Markus Armbruster <armbru@redhat.com> > Thanks! Laszlo
diff --git a/hw/core/sysbus.c b/hw/core/sysbus.c index b53c351..0ebb4e2 100644 --- a/hw/core/sysbus.c +++ b/hw/core/sysbus.c @@ -281,19 +281,15 @@ static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent) static char *sysbus_get_fw_dev_path(DeviceState *dev) { SysBusDevice *s = SYS_BUS_DEVICE(dev); - char path[40]; - int off; - - off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev)); if (s->num_mmio) { - snprintf(path + off, sizeof(path) - off, "@"TARGET_FMT_plx, - s->mmio[0].addr); - } else if (s->num_pio) { - snprintf(path + off, sizeof(path) - off, "@i%04x", s->pio[0]); + return g_strdup_printf("%s@"TARGET_FMT_plx, qdev_fw_name(dev), + s->mmio[0].addr); } - - return g_strdup(path); + if (s->num_pio) { + return g_strdup_printf("%s@i%04x", qdev_fw_name(dev), s->pio[0]); + } + return g_strdup(qdev_fw_name(dev)); } void sysbus_add_io(SysBusDevice *dev, hwaddr addr,
Cc: Markus Armbruster <armbru@redhat.com> Cc: Marcel Apfelbaum <marcel@redhat.com> Cc: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Laszlo Ersek <lersek@redhat.com> --- Notes: v4: - unchanged v3: - new in v3 hw/core/sysbus.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-)