Skip to content

SNTP: configTime server cannot reference temporary char pointer #7056

Closed
@mcspr

Description

@mcspr

Basic Infos

  • This issue complies with the issue POLICY doc.
  • I have read the documentation at readthedocs and the issue is not addressed there.
  • I have tested that the issue is present in current master branch (aka latest git).
  • I have searched the issue tracker for a similar issue.
  • If there is a stack dump, I have decoded it.
  • I have filled out all fields below.

Platform

  • Hardware: ESP8285
  • Core Version: 2.6.3 / [63b73bf]
  • Development Env: PlatformIO
  • Operating System: Linux

Settings in IDE

  • Defaults

Problem Description

With this piece of code below:

sntp_setservername(id, (char*) name_or_ip);

If user does something like:

const String server = retrieveMyServerSettings();
configTime("UTC0", server.c_str());

Because lwip only stores a pointer
https://git.savannah.nongnu.org/cgit/lwip.git/tree/src/apps/sntp/sntp.c?h=STABLE-2_1_x#n876

void
sntp_setservername(u8_t idx, const char *server)
{
  LWIP_ASSERT_CORE_LOCKED();
  if (idx < SNTP_MAX_SERVERS) {
    sntp_servers[idx].name = server;
  }
}

Pointer becomes garbage after some time.

Library example is not affected

MCVE Sketch

#include <ESP8266WiFi.h>
#include <Arduino.h>
#include <sntp.h>
#include <PolledTimeout.h>

static esp8266::polledTimeout::periodicFastMs poll(1000);

void setup() {
    Serial.begin(115200);
    String value("testtesttest");
    configTime("UTC0", value.c_str(), "domainname");
    Serial.printf("before: 0 -> %s, 1 -> %s\n", sntp_getservername(0), sntp_getservername(1));
    WiFi.persistent(false);
    WiFi.mode(WIFI_STA);
    WiFi.begin("blah", "blah");
}

void loop() {
    if (poll) {
        Serial.printf("1 -> %s\n", sntp_getservername(1));
        Serial.flush();
        Serial.printf("0 -> %s\n", sntp_getservername(0));
        Serial.flush();
    }
}

Debug Messages

// 2.6.3 even crashes. git manages to stay online

before: testtesttest domainname
1 -> domainname
0 -> ␔��?�:
1 -> domainname
0 -> ␔��?�:
1 -> domainname
0 -> ␔��?�:

Exception (28):
epc1=0x40207c2e epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000
...

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions