esp8266 / Arduino

ESP8266 core for Arduino
GNU Lesser General Public License v2.1
16.01k stars 13.33k forks source link

multiple char [] , last defined char[] would affect the previous one (pointer issue ?) #2071

Closed mohsh86 closed 8 years ago

mohsh86 commented 8 years ago

Basic Infos

this sketch let ESP8266 to connect to energy meter PZEM-004T to retrieve energy usage data, in this instance, the PZEM-004T was disconnected for testing purposes, which results in -1 return for all the values by the PZEM-004T library, this demo was created from a combo of esp8266 wifi connect + mqtt pubsub + PZEM examples.

since there is no float to char []* in esp8266, it was converted to string first.

Update the code was stripped from mqtt, wifi and serial comms for simplicity

Hardware

Hardware: Wemos D1 Mini Core Version: 2.2

Description

defining multiple char [] arrays: msg, vchar, ichar, pchar, echar, rchar once i introduced rchar (of size 1) programming wise might be wrong but for the sake of the bug i noticed that the energy reading (watthour generated by echar[]) is outputting blank on println instead of -1. [swapping the position of pchar and echar in decleration, pchar got affected] increasing the size of rchar (to say rchar [10] like the others) would fix the issue, it looks like array size pointer index is referring to the previous declared ones ? i've abandoned programming ages ago so pardon me for bad programming in this sketch or not explaining the bug very well

Settings in IDE

Module: Generic ESP8266 Module Flash Size: 4MB/1MB CPU Frequency: 80Mhz Flash Mode: qio Flash Frequency: 40Mhz Upload Using: SERIAL Reset Method: nodemcu

Sketch

`float v=-1,i=-1,p=-1,e=-1; int r=0; String v_str,i_str,p_str,e_str,r_str; char vchar[10],ichar[10],pchar[10],echar[10]; char rchar[1];

void setup() { Serial.begin(115200); }

void loop() { v_str=String(v); i_str=String(i); p_str=String(p); e_str=String(e); r_str=String(r);

v_str.toCharArray(vchar, v_str.length() + 1); i_str.toCharArray(ichar, i_str.length() + 1); p_str.toCharArray(pchar, p_str.length() + 1); e_str.toCharArray(echar, e_str.length() + 1); r_str.toCharArray(rchar, r_str.length() + 1);

Serial.print("Publish message voltage ");
Serial.println(vchar);

Serial.print("Publish message current ");
Serial.println(ichar);

Serial.print("Publish message watt ");
Serial.println(pchar);

Serial.print("Publish message watthour ");
Serial.println(echar);

Serial.print("Publish message reset ");
Serial.println(rchar);

delay(1000);

}`

Debug Messages

Publish message voltage -1.00 Publish message current -1.00 Publish message watt -1.00 Publish message watthour Publish message reset 0

Note

watthour should be -1.00

WereCatf commented 8 years ago

once i introduced rchar (of size 1) programming wise might be wrong but for the sake of the bug i noticed that the energy reading (watthour generated by echar[]) is outputting blank on println instead of -1. [swapping the position of pchar and echar in decleration, pchar got affected]

Buffer-overflow, it's as simple as that. The bug is not with the ESP8266 codebase, the bug is simply in your own code: increase the size of rchar so it doesn't overflow.

mohsh86 commented 8 years ago

rchar (for reset) is an int (after type conversion) that will always have either 1 or 0 in my program, my question is, setting the size of rchar would result in affecting the previous array (echar)

WereCatf commented 8 years ago

I just told you, it's a buffer-overflow. You are not reserving enough space for the character and the terminating NULL, so the NULL ends up in echar instead. Increase the size of rchar.