khoih-prog / WiFiWebServer

This is simple yet complete WebServer library for AVR, Portenta_H7, Teensy, SAM DUE, SAMD, STM32, RP2040-based, etc. boards running WiFi modules/shields (WiFiNINA, CYW43439, U-Blox W101, W102, etc.). The functions are similar and compatible to ESP8266/ESP32 WebServer libraries to make life much easier to port sketches from ESP8266/ESP32. Now using WiFiMulti_Generic library
MIT License
104 stars 21 forks source link

More pointer initialization ( = nullptr) #7

Closed bizprof closed 3 years ago

bizprof commented 3 years ago

That should be it for WiFiWebServer.

bizprof commented 3 years ago

There weren't many more uninitialised pointers, thankfully! Hope it makes the code more robust. Saw in the code that maximum number of parameters is set to 32 in Parsing-impl.h, might want to mention that in the documentation somewhere, too. If I recall correctly, it is once hardcoded to 32, and once to WEBSERVER_MAX_POST_ARGS, which might create issues in the future if somebody tries to increase this parameter.

khoih-prog commented 3 years ago

The pointers actually are initialized in constructor as

WiFiWebServer::WiFiWebServer(int port)
  : _server(port)
  , _currentMethod(HTTP_ANY)
  , _currentVersion(0)
  , _currentHandler(0)
  , _firstHandler(0)
  , _lastHandler(0)
  , _currentArgCount(0)
  , _currentArgs(0)
  , _headerKeysCount(0)
  , _currentHeaders(0)
  , _contentLength(0)
  , _chunked(false)
{
}

So it's better to modify the constructor as follows

WiFiWebServer::WiFiWebServer(int port)
  : _server(port)
  , _currentMethod(HTTP_ANY)
  , _currentVersion(0)
  , _currentHandler(nullptr)
  , _firstHandler(nullptr)
  , _lastHandler(nullptr)
  , _currentArgCount(0)
  , _currentArgs(nullptr)
  , _headerKeysCount(0)
#if USE_NEW_WEBSERVER_VERSION
  ,_currentUpload(nullptr)
  ,_postArgs(nullptr)
#endif
  , _currentHeaders(nullptr)
  , _contentLength(0)
  , _chunked(false)
{
}

I'll fix it

bizprof commented 3 years ago

That's great, didn't think of checking the constructor at all. In https://github.com/khoih-prog/WiFiWebServer/blob/master/src/WiFiWebServer.h, sorry about the typo with the (extra) line 350, must have been hitting ^V twice... Looks like you commented it out, but it still shows in the code somehow?

khoih-prog commented 3 years ago

Will fix later. I'm currently checking to see what else. There are errors in 2nd PR.

khoih-prog commented 3 years ago

No, we don't need in WiFi_HttpClient.h

static const char* kUserAgent = nullptr;
static const char* kContentLengthPrefix = nullptr;
static const char* kTransferEncodingChunked = nullptr;

because we already have in WiFi_HttpClient.cpp

const char* WiFiHttpClient::kUserAgent = "Arduino/2.2.0";
const char* WiFiHttpClient::kContentLengthPrefix = HTTP_HEADER_CONTENT_LENGTH ": ";
const char* WiFiHttpClient::kTransferEncodingChunked = HTTP_HEADER_TRANSFER_ENCODING ": " HTTP_HEADER_VALUE_CHUNKED;
bizprof commented 3 years ago

Sorry about that. The "const" should have made it clear to me that a value gets assigned only once... No use creating PRs when one is tired :-) I have deleted my fork already, will just clone again when needed.

khoih-prog commented 3 years ago

No use creating PRs when one is tired :-)

Happens to me too. No use merging PRs when one is tired :-) We definitely need some fresh air and a sip of fresh coffee. ;={

bizprof commented 3 years ago

Yes, absolutely, and it's time to call it a day for me. Only 6:30pm over here, but can't think straight anymore :-) Off to brew a strong cup of coffee!

So glad we got that issue out of the way. Something I will from now on also check as I look at other code. Tomorrow I'll spend some more time on the WiFiManager again.

By the way, thanks for all your help and support, Khoi! I'm still learning C++, and it's not the easiest of languages for noobs like me...

khoih-prog commented 3 years ago

it's not the easiest of languages for noobs like me...

No, you're not anymore. But learning something new everyday is good, even for me, and other experienced people. I love the old and wise ways which keeps me learning everyday

You know nothing, if you're thinking and saying you know everything.
Your knowledge is just a small grain of sand on the beach