ARMmbed / ARMmbed/ntp-client

time_t NTPClient::get_timestamp() incorrect error logic

Open
#13 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
7
Forks
12
PR merge metrics
No merged PRs in 30d

Description

The problem:
time_t is defined as:

typedef unsigned int time_t; /* date/time in unix secs past 1-Jan-70 */

therefore, get_timestamp() can't return a negative value on error.
Instead, it should return 0 on error or be changed as follows to be consistent with error report in other Mbed OS modules:
```

int NTPClient::get_timestamp(time_t *time, int timeout)
{
const time_t TIME1970 = (time_t)2208988800UL;
int ntp_send_values[12] = {0};
int ntp_recv_values[12] = {0};

SocketAddress nist;

if (iface)
{
int ret_gethostbyname = iface->gethostbyname(nist_server_address, &nist);

if (ret_gethostbyname < 0) {
// Network error on DNS lookup
return ret_gethostbyname;
}

nist.set_port(nist_server_port);

memset(ntp_send_values, 0x00, sizeof(ntp_send_values));
ntp_send_values[0] = '\x1b';

memset(ntp_recv_values, 0x00, sizeof(ntp_recv_values));

UDPSocket sock;
sock.open(iface);
sock.set_timeout(timeout);

sock.sendto(nist, (void*)ntp_send_values, sizeof(ntp_send_values));

SocketAddress source;
const int n = sock.recvfrom(&source, (void*)ntp_recv_values, sizeof(ntp_recv_values));

if (n > 10) /* success */
{
*time = ntohl(ntp_recv_values[10]) - TIME1970;

return NSAPI_ERROR_OK; /* return 0 if no error */

} else {
if (n < 0)
{
// Network error
return n;

} else {
// No or partial data returned
return -1;
}
}

} else {
// No network interface
return -2;
}
}

```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at the NTPClient::get_timestamp() entry point and inspect its callers to understand the expected error convention. Reproduce the failure around the unsigned time_t return behavior, then verify that the chosen error handling is consistent with callers and existing Mbed OS module conventions.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
networking
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.