PaulStoffregen / Time

Time library for Arduino
http://playground.arduino.cc/code/time
1.24k stars 664 forks source link

getNtpTime() doesn't handle zero from NTP server response #174

Open Anton-V-K opened 1 year ago

Anton-V-K commented 1 year ago

The code of getNtpTime() in https://github.com/PaulStoffregen/Time/blob/master/examples/TimeNTP/TimeNTP.ino doesn't check the correctness of UDP packet's content (obtained from NTP server), and can potentially set invalid date (in future - 07-Feb-2036). I'm referring to this code fragment:

if (size >= NTP_PACKET_SIZE) {
      Serial.println("Receive NTP Response");
      Udp.read(packetBuffer, NTP_PACKET_SIZE);  // read packet into the buffer
      unsigned long secsSince1900;
      // convert four bytes starting at location 40 to a long integer
      secsSince1900 =  (unsigned long)packetBuffer[40] << 24;
      secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
      secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
      secsSince1900 |= (unsigned long)packetBuffer[43];
      return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
}

Here secsSince1900 can be zero, when in certain cases ( like "rate limiting" - mentioned in https://github.com/arduino-libraries/NTPClient/issues/84 ) NTP responds with zero time.

gednz commented 5 months ago

I am having this issue too