Skip to content

Commit 7d387dd

Browse files
nwfmarcelstoer
authored andcommittedJul 27, 2019
Simplify and tidy SNTP (#2700)
* list_ref can become LUA_REFNIL, because that's what rawgeti returns for LUA_NOREF. Defensively guard for this, rather than falling into the sntp_dolookups loop with nil on the stack. * set_repeat_mode should not call itself, but should rather always do what it's going to do and then optionally do the rest if directed. * sntp_sync should not try to special case the single string argument: we should be queueing that name for DNS resolution, too. Towards that end, if we are given a single string, build a table and make that the list_ref and call off to sntp_dolookups, just like we otherwise do. FIXES: #2699
1 parent 3d30c98 commit 7d387dd

File tree

1 file changed

+19
-27
lines changed

1 file changed

+19
-27
lines changed
 

‎app/modules/sntp.c

+19-27
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ static void sntp_dolookups (lua_State *L) {
627627
// Step through each element of the table, converting it to an address
628628
// at the end, start the lookups. If we have already looked everything up,
629629
// then move straight to sending the packets.
630-
if (state->list_ref == LUA_NOREF) {
630+
if ((state->list_ref == LUA_NOREF) || (state->list_ref == LUA_REFNIL)) {
631631
sntp_dosend();
632632
return;
633633
}
@@ -702,8 +702,16 @@ static char *state_init(lua_State *L) {
702702

703703
static char *set_repeat_mode(lua_State *L, bool enable)
704704
{
705+
if (repeat) {
706+
os_timer_disarm (&repeat->timer);
707+
luaL_unref (L, LUA_REGISTRYINDEX, repeat->sync_cb_ref);
708+
luaL_unref (L, LUA_REGISTRYINDEX, repeat->err_cb_ref);
709+
luaL_unref (L, LUA_REGISTRYINDEX, repeat->list_ref);
710+
free(repeat);
711+
repeat = NULL;
712+
}
713+
705714
if (enable) {
706-
set_repeat_mode(L, FALSE);
707715
repeat = (sntp_repeat_t *) malloc(sizeof(sntp_repeat_t));
708716
if (!repeat) {
709717
return "no memory";
@@ -720,16 +728,8 @@ static char *set_repeat_mode(lua_State *L, bool enable)
720728
//The function on_long_timeout returns errors to the developer
721729
//My guess: Error reporting is a good thing, resume the timer.
722730
os_timer_arm(&repeat->timer, 1000 * 1000, 1);
723-
} else {
724-
if (repeat) {
725-
os_timer_disarm (&repeat->timer);
726-
luaL_unref (L, LUA_REGISTRYINDEX, repeat->sync_cb_ref);
727-
luaL_unref (L, LUA_REGISTRYINDEX, repeat->err_cb_ref);
728-
luaL_unref (L, LUA_REGISTRYINDEX, repeat->list_ref);
729-
free(repeat);
730-
repeat = NULL;
731-
}
732731
}
732+
733733
return NULL;
734734
}
735735

@@ -791,22 +791,17 @@ static int sntp_sync (lua_State *L)
791791
if (lua_istable(L, 1)) {
792792
// Save a reference to the table
793793
lua_pushvalue(L, 1);
794-
luaL_unref (L, LUA_REGISTRYINDEX, state->list_ref);
795-
state->list_ref = luaL_ref(L, LUA_REGISTRYINDEX);
796-
sntp_dolookups(L);
797-
goto good_ret;
798794
} else {
799795
size_t l;
800796
const char *hostname = luaL_checklstring(L, 1, &l);
801797
if (l>128 || hostname == NULL)
802798
sync_err("need <128 hostname");
803-
err_t err = dns_gethostbyname(hostname, get_free_server(), sntp_dns_found, state);
804-
if (err == ERR_INPROGRESS) {
805-
goto good_ret;
806-
} else if (err == ERR_ARG)
807-
sync_err("bad hostname");
808799

809-
server_count++;
800+
/* Construct a singleton table containing the one server */
801+
lua_newtable(L);
802+
lua_pushnumber(L, 1);
803+
lua_pushstring(L, hostname);
804+
lua_settable(L, -3);
810805
}
811806
} else if (server_count == 0) {
812807
lua_newtable(L);
@@ -827,15 +822,12 @@ static int sntp_sync (lua_State *L)
827822
lua_settable(L, -3);
828823
}
829824
}
830-
luaL_unref (L, LUA_REGISTRYINDEX, state->list_ref);
831-
state->list_ref = luaL_ref(L, LUA_REGISTRYINDEX);
832-
sntp_dolookups(L);
833-
goto good_ret;
834825
}
835826

836-
sntp_dosend ();
827+
luaL_unref (L, LUA_REGISTRYINDEX, state->list_ref);
828+
state->list_ref = luaL_ref(L, LUA_REGISTRYINDEX);
829+
sntp_dolookups(L);
837830

838-
good_ret:
839831
if (!lua_isnoneornil(L, 4)) {
840832
set_repeat_mode(L, 1);
841833
}

0 commit comments

Comments
 (0)
Failed to load comments.