Skip to content

sprintf statement only gives compile error if compiled with compiler warning level "All" and "More" (-Wall -Werror=all) #7024

Closed
@ArminJo

Description

@ArminJo

Board

compiler problem

Device Description

compiler problem

Hardware Configuration

compiler problem

Version

v2.0.4

IDE Name

Arduino, Sloeber

Operating System

%

Flash frequency

compiler problem

PSRAM enabled

no

Upload speed

compiler problem

Description

The sketch below throws the error

E:\WORKSPACE_ARDUINO\ArduinoSketches\sketch_jul21a\sketch_jul21a.ino: In function 'void setup()':
sketch_jul21a:7:26: error: '%s' directive writing up to 19 bytes into a region of size 14 [-Werror=format-overflow=]
   sprintf(sStringBuffer, "Acc.X %s", &sStringBuffer[10]);
   ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
E:\WORKSPACE_ARDUINO\ArduinoSketches\sketch_jul21a\sketch_jul21a.ino:7:10: note: 'sprintf' output between 7 and 26 bytes into a destination of size 20
   sprintf(sStringBuffer, "Acc.X %s", &sStringBuffer[10]);
   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1plus.exe: some warnings being treated as errors
exit status 1
'%s' directive writing up to 19 bytes into a region of size 14 [-Werror=format-overflow=]

if compiled with -Wall -Werror=all

For all non ESP32 platforms it compiles without any error! - see https://github.com/ArminJo/Arduino-BlueDisplay/actions/runs/2714739545

Sketch

char sStringBuffer[20];

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  sprintf(&sStringBuffer[10], "123.4567"); // edited: original code was: dtostrf(123.45, 4, 1, &sStringBuffer[10]);
  sprintf(sStringBuffer, "Acc.X %s", &sStringBuffer[10]);
  Serial.println(sStringBuffer);
}

void loop() {
  // put your main code here, to run repeatedly:
}

Debug Message

E:\WORKSPACE_ARDUINO\ArduinoSketches\sketch_jul21a\sketch_jul21a.ino: In function 'void setup()':
sketch_jul21a:7:26: error: '%s' directive writing up to 19 bytes into a region of size 14 [-Werror=format-overflow=]
   sprintf(sStringBuffer, "Acc.X %s", &sStringBuffer[10]);
   ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
E:\WORKSPACE_ARDUINO\ArduinoSketches\sketch_jul21a\sketch_jul21a.ino:7:10: note: 'sprintf' output between 7 and 26 bytes into a destination of size 20
   sprintf(sStringBuffer, "Acc.X %s", &sStringBuffer[10]);
   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1plus.exe: some warnings being treated as errors
exit status 1
'%s' directive writing up to 19 bytes into a region of size 14 [-Werror=format-overflow=]


### Other Steps to Reproduce

_No response_

### I have checked existing issues, online documentation and the Troubleshooting Guide

- [X] I confirm I have checked existing issues, online documentation and Troubleshooting guide.

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions