How to correction this please

Hi everyone,
the error: '%04X' directive writing 4 bytes into a region of size between 2 and 4 [-Werror=format-overflow=]
how to fix please.
Thanks
Adam

#include <Wire.h>
#include "ClosedCube_HDC1080.h"

ClosedCube_HDC1080 hdc1080;

void setup()
{
	Serial.begin(9600);
	Serial.println("ClosedCube HDC1080 Arduino Test");

	// Default settings: 
	//  - Heater off
	//  - 14 bit Temperature and Humidity Measurement Resolutions
	hdc1080.begin(0x40);

	Serial.print("Manufacturer ID=0x");
	Serial.println(hdc1080.readManufacturerId(), HEX); // 0x5449 ID of Texas Instruments
	Serial.print("Device ID=0x");
	Serial.println(hdc1080.readDeviceId(), HEX); // 0x1050 ID of the device
	
	printSerialNumber();

}

void loop()
{
	Serial.print("T=");
	Serial.print(hdc1080.readTemperature());
	Serial.print("C, RH=");
	Serial.print(hdc1080.readHumidity());
	Serial.println("%");
	delay(3000);
}

void printSerialNumber() {
	Serial.print("Device Serial Number=");
	HDC1080_SerialNumber sernum = hdc1080.readSerialNumber();
	char format[12];
sprintf(format, "%02X-%04X-%04X", sernum.serialFirst, sernum.serialMid, sernum.serialLast);

 sprintf(format, "%02X,%02X, %02X", sernum.serialFirst, sernum.serialMid, sernum.serialLast);
	Serial.println(format);
}

error:

Arduino: 1.8.19 (Windows 7), Board: "AI Thinker ESP32-CAM, 160MHz (WiFi/BT), QIO, Huge APP (3MB No OTA/1MB SPIFFS), 40MHz, None, Disabled"

C:\Users\HUA.DELLV-PC\Downloads\ClosedCube_HDC1080_Arduino-master\ClosedCube_HDC1080_Arduino-master\examples\ESP32_HDC1080_demo\ESP32_HDC1080_demo.ino: In function 'void printSerialNumber()':

ESP32_HDC1080_demo:60:17: error: '%04X' directive writing 4 bytes into a region of size between 2 and 4 [-Werror=format-overflow=]

 sprintf(format, "%02X-%04X-%04X", sernum.serialFirst, sernum.serialMid, sernum.serialLast);

                 ^~~~~~~~~~~~~~~~

C:\Users\HUA.DELLV-PC\Downloads\ClosedCube_HDC1080_Arduino-master\ClosedCube_HDC1080_Arduino-master\examples\ESP32_HDC1080_demo\ESP32_HDC1080_demo.ino:60:17: note: directive argument in the range [0, 65535]

C:\Users\HUA.DELLV-PC\Downloads\ClosedCube_HDC1080_Arduino-master\ClosedCube_HDC1080_Arduino-master\examples\ESP32_HDC1080_demo\ESP32_HDC1080_demo.ino:60:8: note: 'sprintf' output between 13 and 15 bytes into a destination of size 12

 sprintf(format, "%02X-%04X-%04X", sernum.serialFirst, sernum.serialMid, sernum.serialLast);

 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ESP32_HDC1080_demo:62:18: error: '%02X' directive writing between 2 and 4 bytes into a region of size between 1 and 5 [-Werror=format-overflow=]

  sprintf(format, "%02X,%02X, %02X", sernum.serialFirst, sernum.serialMid, sernum.serialLast);

                  ^~~~~~~~~~~~~~~~~

C:\Users\HUA.DELLV-PC\Downloads\ClosedCube_HDC1080_Arduino-master\ClosedCube_HDC1080_Arduino-master\examples\ESP32_HDC1080_demo\ESP32_HDC1080_demo.ino:62:18: note: directive argument in the range [0, 65535]

C:\Users\HUA.DELLV-PC\Downloads\ClosedCube_HDC1080_Arduino-master\ClosedCube_HDC1080_Arduino-master\examples\ESP32_HDC1080_demo\ESP32_HDC1080_demo.ino:62:9: note: 'sprintf' output between 10 and 16 bytes into a destination of size 12

  sprintf(format, "%02X,%02X, %02X", sernum.serialFirst, sernum.serialMid, sernum.serialLast);

  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

cc1plus.exe: some warnings being treated as errors

exit status 1

'%04X' directive writing 4 bytes into a region of size between 2 and 4 [-Werror=format-overflow=]



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Please show us the source for this struct.

1 Like

Thanks.
the whole stuff from:
http://www.esp32learning.com/code/esp32-and-hdc1080-humidity-and-temperature-sensor.php

It's not my job to find it. It's yours. Use a text editor "find".

1 Like

this one? from ClosedCube_HDC1080.h

typedef union {
	uint8_t rawData[6];
	struct {
		uint16_t serialFirst;
		uint16_t serialMid;
		uint16_t serialLast;
	};
} HDC1080_SerialNumber;

Hello

char format[12];

This is not enough to store a string like "XX-XXXX-XXXX" which is 13 characters

1 Like

See, can you represent a uint16_t in 2 digits? :slight_smile:

1 Like

Great!
thank you.
changed to [24] , It works.

Thank you.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.