Skip to contentSkip to conversation

exynos4210_gic: Suppress gcc9 format-truncation warnings

PATCH In review David Gibson 3 participants 4 messages Dec 2, 2019 – Dec 3, 2019 Arm Samsung Exynos

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 exynos4210_gic: Suppress gcc9 format-truncation warnings

exynos4210_gic_realize() prints the number of cpus into some temporary buffers, but it only allows 3 bytes space for it. That's plenty:
existing machines will only ever set this value to EXYNOS4210_NCPUS (2). But the compiler can't always figure that out, so some[*] gcc9 versions emit -Wformat-truncation warnings.

We can fix that by hinting the constraint to the compiler with a suitably placed assert().

[*] The bizarre thing here, is that I've long gotten these warnings

    compiling in a 32-bit x86 container as host - Fedora 30 with
    gcc-9.2.1-1.fc30.i686 - but it compiles just fine on my normal
    x86_64 host - Fedora 30 with and gcc-9.2.1-1.fc30.x86_64.

Peter, up to you if you squeeze this in for qemu-4.2 or leave it until 5.0

Changes since v2:
* Moved the assert outside the for loop using a trick suggested by Richard Henderson
Changes since v1:
* Used an assert to hint the compiler, instead of increasing the buffer size.

---
 hw/intc/exynos4210_gic.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
hw/intc/exynos4210_gic.c +9 -1
index a1b699b6ba..ddd006aca6 100644
@@ -293,6 +293,7 @@ static void exynos4210_gic_realize(DeviceState *dev, Error **errp)
     char cpu_alias_name[sizeof(cpu_prefix) + 3];
     char dist_alias_name[sizeof(cpu_prefix) + 3];
     SysBusDevice *gicbusdev;
+    uint32_t n = s->num_cpu;
     uint32_t i;
 
     s->gic = qdev_create(NULL, "arm_gic");
@@ -313,7 +314,14 @@ static void exynos4210_gic_realize(DeviceState *dev, Error **errp)
     memory_region_init(&s->dist_container, obj, "exynos4210-dist-container",
             EXYNOS4210_EXT_GIC_DIST_REGION_SIZE);
 
-    for (i = 0; i < s->num_cpu; i++) {
+    /*
+     * This clues in gcc that our on-stack buffers do, in fact have
+     * enough room for the cpu numbers.  gcc 9.2.1 on 32-bit x86
+     * doesn't figure this out, otherwise and gives spurious warnings.
+     */
+    assert(n <= EXYNOS4210_NCPUS);
+    for (i = 0; i < n; i++) {
+
         /* Map CPU interface per SMP Core */
         sprintf(cpu_alias_name, "%s%x", cpu_prefix, i);
         memory_region_init_alias(&s->cpu_alias[i], obj,
Signature · 1 line
2.23.0
Reviewed-by on v3 1/1
#1 lore raw

hw/intc/exynos4210_gic.c static void exynos4210_gic_realize(DeviceState *dev, Error **errp) · line 325

Quoted 11 lines · David Gibson
hw/intc/exynos4210_gic.c +8 -1
-    for (i = 0; i < s->num_cpu; i++) {
+    /*
+     * This clues in gcc that our on-stack buffers do, in fact have
+     * enough room for the cpu numbers.  gcc 9.2.1 on 32-bit x86
+     * doesn't figure this out, otherwise and gives spurious warnings.
+     */
+    assert(n <= EXYNOS4210_NCPUS);
+    for (i = 0; i < n; i++) {
+
         /* Map CPU interface per SMP Core */

Watch out for the extra line added at the start of the block. Otherwise,

r~

#2 lore raw
Quoted 14 lines · Richard Henderson

hw/intc/exynos4210_gic.c static void exynos4210_gic_realize(DeviceState *dev, Error **errp) · line 325

Quoted 11 lines · David Gibson
hw/intc/exynos4210_gic.c +8 -1
-    for (i = 0; i < s->num_cpu; i++) {
+    /*
+     * This clues in gcc that our on-stack buffers do, in fact have
+     * enough room for the cpu numbers.  gcc 9.2.1 on 32-bit x86
+     * doesn't figure this out, otherwise and gives spurious warnings.
+     */
+    assert(n <= EXYNOS4210_NCPUS);
+    for (i = 0; i < n; i++) {
+
         /* Map CPU interface per SMP Core */
Quoted 3 lines

Watch out for the extra line added at the start of the block. Otherwise,

I thought about putting this in rc4 but eventually decided against it. Queued for 5.0 (with the stray extra blank line removed).

thanks
-- PMM

#3 lore raw
Quoted 16 lines · Peter Maydell
Quoted 14 lines · Richard Henderson
Quoted 11 lines · David Gibson
hw/intc/exynos4210_gic.c +8 -1
-    for (i = 0; i < s->num_cpu; i++) {
+    /*
+     * This clues in gcc that our on-stack buffers do, in fact have
+     * enough room for the cpu numbers.  gcc 9.2.1 on 32-bit x86
+     * doesn't figure this out, otherwise and gives spurious warnings.
+     */
+    assert(n <= EXYNOS4210_NCPUS);
+    for (i = 0; i < n; i++) {
+
         /* Map CPU interface per SMP Core */
Quoted 7 lines
Quoted 3 lines

Watch out for the extra line added at the start of the block. Otherwise,

I thought about putting this in rc4 but eventually decided against it. Queued for 5.0 (with the stray extra blank line removed).

Great!

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson