From 4cb8861070eff0ca43c1d80d1b31528c878f4069 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Fri, 22 Dec 2017 21:28:28 -0800 Subject: [PATCH] Fix PROGMEM string accesses in callbacks The MD and STATUS callbacks can be passed PROGMEM strings, so be sure to use proper strcpy_p()s to move them to RAM before printing to avoid crashes. Found during debug of issue #21 . --- .../StreamMP3FromHTTP/StreamMP3FromHTTP.ino | 17 ++++++++++++----- .../StreamMP3FromHTTP_SPIRAM.ino | 17 ++++++++++++----- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/examples/StreamMP3FromHTTP/StreamMP3FromHTTP.ino b/examples/StreamMP3FromHTTP/StreamMP3FromHTTP.ino index ef3fa89..da48487 100644 --- a/examples/StreamMP3FromHTTP/StreamMP3FromHTTP.ino +++ b/examples/StreamMP3FromHTTP/StreamMP3FromHTTP.ino @@ -25,7 +25,13 @@ void MDCallback(void *cbData, const char *type, bool isUnicode, const char *stri { const char *ptr = reinterpret_cast(cbData); (void) isUnicode; // Punt this ball for now - Serial.printf("METADATA(%s) '%s' = '%s'\n", ptr, type, string); + // Note that the type and string may be in PROGMEM, so copy them to RAM for printf + char s1[32], s2[64]; + strncpy_P(s1, type, sizeof(s1)); + s1[sizeof(s1)-1]=0; + strncpy_P(s2, string, sizeof(s2)); + s2[sizeof(s2)-1]=0; + Serial.printf("METADATA(%s) '%s' = '%s'\n", ptr, s1, s2); Serial.flush(); } @@ -33,7 +39,11 @@ void MDCallback(void *cbData, const char *type, bool isUnicode, const char *stri void StatusCallback(void *cbData, int code, const char *string) { const char *ptr = reinterpret_cast(cbData); - Serial.printf("STATUS(%s) '%d' = '%s'\n", ptr, code, string); + // Note that the string may be in PROGMEM, so copy it to RAM for printf + char s1[64]; + strncpy_P(s1, string, sizeof(s1)); + s1[sizeof(s1)-1]=0; + Serial.printf("STATUS(%s) '%d' = '%s'\n", ptr, code, s1); Serial.flush(); } @@ -50,9 +60,6 @@ void setup() WiFi.hostname("melody"); - byte zero[] = {0,0,0,0}; - WiFi.config(zero, zero, zero, zero); - WiFi.begin(SSID, PASSWORD); // Try forever diff --git a/examples/StreamMP3FromHTTP_SPIRAM/StreamMP3FromHTTP_SPIRAM.ino b/examples/StreamMP3FromHTTP_SPIRAM/StreamMP3FromHTTP_SPIRAM.ino index f8ad1f3..bdb1d28 100644 --- a/examples/StreamMP3FromHTTP_SPIRAM/StreamMP3FromHTTP_SPIRAM.ino +++ b/examples/StreamMP3FromHTTP_SPIRAM/StreamMP3FromHTTP_SPIRAM.ino @@ -24,7 +24,13 @@ void MDCallback(void *cbData, const char *type, bool isUnicode, const char *stri { const char *ptr = reinterpret_cast(cbData); (void) isUnicode; // Punt this ball for now - Serial.printf("METADATA(%s) '%s' = '%s'\n", ptr, type, string); + // Note that the type and string may be in PROGMEM, so copy them to RAM for printf + char s1[32], s2[64]; + strncpy_P(s1, type, sizeof(s1)); + s1[sizeof(s1)-1]=0; + strncpy_P(s2, string, sizeof(s2)); + s2[sizeof(s2)-1]=0; + Serial.printf("METADATA(%s) '%s' = '%s'\n", ptr, s1, s2); Serial.flush(); } @@ -32,7 +38,11 @@ void MDCallback(void *cbData, const char *type, bool isUnicode, const char *stri void StatusCallback(void *cbData, int code, const char *string) { const char *ptr = reinterpret_cast(cbData); - Serial.printf("STATUS(%s) '%d' = '%s'\n", ptr, code, string); + // Note that the string may be in PROGMEM, so copy it to RAM for printf + char s1[64]; + strncpy_P(s1, string, sizeof(s1)); + s1[sizeof(s1)-1]=0; + Serial.printf("STATUS(%s) '%d' = '%s'\n", ptr, code, s1); Serial.flush(); } @@ -48,9 +58,6 @@ void setup() WiFi.hostname("melody"); - byte zero[] = {0,0,0,0}; - WiFi.config(zero, zero, zero, zero); - WiFi.begin(SSID, PASSWORD); // Try forever