Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash in serializeJson with Empty Values after Update (Arduino-ESP32 3.1.2) #10971

Closed
1 task done
hosseinaghaie opened this issue Feb 15, 2025 · 7 comments · Fixed by #10972
Closed
1 task done

Crash in serializeJson with Empty Values after Update (Arduino-ESP32 3.1.2) #10971

hosseinaghaie opened this issue Feb 15, 2025 · 7 comments · Fixed by #10972
Labels
bug 🐞 Inconsistencies or issues which will cause a problem for users or implementers. Status: Pending Merge Pull Request is ready to be merged Type: Regression Result of unforeseen consequences of a previous change

Comments

@hosseinaghaie
Copy link

hosseinaghaie commented Feb 15, 2025

Board

Board: ESP32-S3

Device Description

Description:
After updating to Arduino-ESP32 3.1.2, we observed frequent crashes when using serializeJson with DynamicJsonDocument. The issue occurs specifically when handling empty or minimal JSON objects like {} or when the serialized String is empty (""). This behavior was not present in previous versions and might be related to recent changes in String behavior (String::move fix). The crash generates a Guru Meditation Error (LoadProhibited).

Example Code:
DynamicJsonDocument doc(2048);
doc["data"] = "{}";
String json;
serializeJson(doc, json); // Causes crash

We also experience similar crashes when serializing responses with minimal data (doc["data"] = {} or []), which should be valid JSON structures.

By switching to std::string for handling serialization, the crash is avoided. The modified code is shown below:

#include // Use std::string

DynamicJsonDocument doc(2048);
doc["data"] = "{}";
std::string jsonString;
serializeJson(doc, jsonString); // No crash
This might suggest an underlying issue with String conversion in the recent core update.

Environment:
Board: ESP32-S3
Core Version: Arduino-ESP32 3.1.2
ArduinoJson Version: 7.3.0
The issue seems closely related to serializeJson combined with String. Let me know if you need additional debug logs or a full backtrace.

Hardware Configuration

no

Version

v3.1.2

IDE Name

Arduino 2.3.4

Operating System

11

Flash frequency

40m

PSRAM enabled

no

Upload speed

115200

Description

no

Sketch

//////////////////////////////////////////////////////////////////////////////////////////////
void processRawInput(String rawData, String source) {
   Serial.println("Raw data to be parsed: " + rawData);

  // Parse JSON from the raw data
  DynamicJsonDocument doc(2048);  // Adjusted for larger data
  DeserializationError error = deserializeJson(doc, rawData);

  if (error) {
    Serial.println("Invalid JSON received from source: " + source);
    return;
  }

  // Extract command details
  CommandData cmdData;
  cmdData.source = source;
  cmdData.sequenceId = doc["sequenceId"] | String(time(NULL));

  if (doc.containsKey("data")) {
    // Serialize the "data" object into a string and store it in cmdData.data
    //String jsonString;
   std::string jsonString;
    serializeJson(doc["data"], jsonString);
   // cmdData.data = jsonString;
    cmdData.data = String(jsonString.c_str());  // Convert safely back to String
  } else {
    Serial.println("Error: 'data' key is missing or invalid.");
    return;
  }

  String commandName = doc["command"] | "";
  if (commandName.isEmpty()) {
    Serial.println("Error: 'command' key is missing or invalid.");
    return;
  }

  // Process the command
  processCommand(commandName, cmdData);
  
}




### Debug Message

```plain
Decoding stack results
0x420405c0:  is in String::operator=(char const*) (C:\Users\Admin\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.1.2\cores\esp32\WString.cpp:293).
0x42016ddf:  is in ArduinoJson::V730PB22::detail::serialize<ArduinoJson::V730PB22::detail::JsonSerializer, String>(ArduinoJson::V730PB22::JsonVariantConst, String&) (c:\Users\Admin\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/Serialization/Writers/ArduinoStringWriter.hpp:19).
0x42016e69:  is in ArduinoJson::V730PB22::serializeJson<String, 0>(ArduinoJson::V730PB22::JsonVariantConst, String&) (c:\Users\Admin\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/Json/JsonSerializer.hpp:137).

Other Steps to Reproduce

No response

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

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.
@hosseinaghaie hosseinaghaie added the Status: Awaiting triage Issue is waiting for triage label Feb 15, 2025
@InnuendoPi
Copy link

I also noticed this error. same with pioarduino 53.03.12. switched back to 3.1.1 (53.03.11)

maybe same issue with tzapu/WiFiManager lib: empty WLAN config (start AP mode) ends up in a exception loop, when calling
bool res = wifiManager.autoConnect();
in setup(). same source no probs with 3.1.1

Exception:
Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.

Core 1 register dump:
PC : 0x4008c1ad PS : 0x00060230 A0 : 0x8012c0ec A1 : 0x3ffb20f0
A2 : 0x00000000 A3 : 0xfffffffc A4 : 0x000000ff A5 : 0x0000ff00
A6 : 0x00ff0000 A7 : 0xff000000 A8 : 0x00000000 A9 : 0x3ffb20c0
A10 : 0x3ffc6541 A11 : 0x00000009 A12 : 0x00000009 A13 : 0x0000ff00
A14 : 0x00ff0000 A15 : 0x3ffc6538 SAR : 0x0000000a EXCCAUSE: 0x0000001c
EXCVADDR: 0x00000000 LBEG : 0x4008c1ad LEND : 0x4008c1bd LCOUNT : 0xffffffff

Backtrace: 0x4008c1aa:0x3ffb20f0 0x4012c0e9:0x3ffb2100 0x40125072:0x3ffb2120 0x4012564d:0x3ffb2190 0x40109c80:0x3ffb2200 0x4012efdb:0x3ffb2270 0x400900c2:0x3ffb2290

sorry, cannot use exception decoder (debug flash size grows 139%).

@me-no-dev
Copy link
Member

@TD-er PTAL. This is a consequence of your changes :)

@TD-er
Copy link
Contributor

TD-er commented Feb 15, 2025

Yep, now that I see the crash (+ line nr), it is obvious why it crashes and is indeed due to my changes in this PR:
5488d5d

It should not call strlen() on a nullptr.

String &String::operator=(const char *cstr) {
  return copy(cstr, strlen(cstr));
}

@TD-er
Copy link
Contributor

TD-er commented Feb 15, 2025

Could you test if this will fix it?
#10972
I just edited it in my browser, so not tested yet myself.

@hosseinaghaie
Copy link
Author

Could you test if this will fix it? #10972 I just edited it in my browser, so not tested yet myself.

i rechecked it,the proble solved!

@Jason2866 Jason2866 added Type: Regression Result of unforeseen consequences of a previous change bug 🐞 Inconsistencies or issues which will cause a problem for users or implementers. Status: Pending Merge Pull Request is ready to be merged and removed Status: Awaiting triage Issue is waiting for triage labels Feb 15, 2025
@enjoyneering
Copy link

Just tried the fix. My code stopped crashing with the latest ArduinoJson library.

@InnuendoPi
Copy link

same test result with tzapu/WiFiManager lib: no exceptions
thanks for fast response!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐞 Inconsistencies or issues which will cause a problem for users or implementers. Status: Pending Merge Pull Request is ready to be merged Type: Regression Result of unforeseen consequences of a previous change
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants