0

I was looking at this Arduino code sample (https://buger.dread.cz/simple-esp8266-https-client-without-verification-of-certificate-fingerprint.html):

std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);

client->setFingerprint(fingerprint);

HTTPClient https;

Serial.print("[HTTPS] begin...\n");
if (https.begin(*client, "https://jigsaw.w3.org/HTTP/connection.html")) {  // HTTPS

  Serial.print("[HTTPS] GET...\n");
  // start connection and send HTTP header
  int httpCode = https.GET();

  // httpCode will be negative on error
  if (httpCode > 0) {
    // HTTP header has been send and Server response header has been handled
    Serial.printf("[HTTPS] GET... code: %d\n", httpCode);

    // file found at server
    if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
      String payload = https.getString();
      Serial.println(payload);
    }
  } else {
    Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
  }

  https.end();
} else {
  Serial.printf("[HTTPS] Unable to connect\n");
}

Although the get request succeeds for me, later on there is a crash. Someone else told me that the way the unique_ptr is used is incorrect because we are passing a raw pointer to it. Can someone explain ? I don't have much experience with unique_ptr.

Rahul Iyer
  • 19,924
  • 21
  • 96
  • 190
  • We really need enough code to reproduce the crash. The code shown seems fine. As you say, the crash is later -- in code not shown. – David Schwartz Nov 19 '19 at 05:07
  • Your usage of `unique_ptr` is fine, although you should prefer to use `make_unique` instead of naked `new`. What happens if you make it a raw pointer instead, does your code work fine? – Tas Nov 19 '19 at 05:07
  • What makes you think the crash is related to `client`? Your [previous question](https://stackoverflow.com/questions/58926590/how-to-dispose-of-unique-ptr) seems to address this. – 1201ProgramAlarm Nov 19 '19 at 05:08
  • Did that same someone tell you to try `auto client = std::make_unique();`. I don't think it will matter, but twirl it. – WhozCraig Nov 19 '19 at 05:09
  • The part I find odd about this code is the pass-by-reference (or value) to `https.begin(*client, ...` . Frankly I don't see the need for a unique_ptr here *at all*, but my only exposure to this is the code you posted. Punch this up in a debugger and catch the crash. – WhozCraig Nov 19 '19 at 05:11
  • I think I am the "someone" mentioned from the earlier (deleted?) question. That was based on a much shorter snippet that didn't make the relative lifetimes of `client` and `https` clear and was referring to `https.begin(*client,`, not the argument to `client` constructor, because `https` seems to store a raw pointer/reference to the argument passed to `begin`. The code in this question does not the lifetime problem I thought of, assuming all of this is actually in the same order in one block in the real code. – walnut Nov 19 '19 at 05:35
  • I wouldn't call it incorrect, just [not preferred](https://stackoverflow.com/a/37514627/10957435). –  Nov 19 '19 at 05:41
  • @WhozCraig Since this is meant to run in an arduino, there doesn't seem to be a way to debug (step through the code with breakpoints). I'm relying on writing statements to the serial port to see where it crashes, and it seems to be just after the call to https.get(). – Rahul Iyer Nov 19 '19 at 05:43
  • @uneven_mark The code is pretty much the same, except for some other code which parses the response. Everything works great, except when I add another function to the code, (which is never called), that's when it crashes at https.get(). – Rahul Iyer Nov 19 '19 at 05:44

0 Answers0