Skip to content
This repository was archived by the owner on Apr 13, 2021. It is now read-only.

Commit d269099

Browse files
committed
Merge pull request #338 from pmiettinen/i182-remove-asserts
Replace asserts with invalid value returning
2 parents 3f08a64 + 051b3f6 commit d269099

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

include/libswiftnav/time.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,16 @@
7777
#define GPS_EPOCH 315964800
7878

7979
#define WN_UNKNOWN -1
80+
#define TOW_UNKNOWN -1
8081

8182
/** Structure representing a GPS time. */
8283
typedef struct __attribute__((packed)) {
8384
double tow; /**< Seconds since the GPS start of week. */
8485
s16 wn; /**< GPS week number. */
8586
} gps_time_t;
8687

88+
#define GPS_TIME_UNKNOWN ((gps_time_t){TOW_UNKNOWN, WN_UNKNOWN})
89+
8790
void normalize_gps_time(gps_time_t *t_gps);
8891

8992
time_t gps2time(const gps_time_t *t);

src/time.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,14 @@ gps_time_t glo_time2gps_time(u16 nt, u8 n4, s8 h, s8 m, s8 s)
144144
u8 j = 0;
145145
u16 day_of_year = 0;
146146
u32 glo_year = 0;
147-
gps_time_t gps_t = {0, 0};
147+
gps_time_t gps_t = GPS_TIME_UNKNOWN;
148148

149-
assert(n4 >= N4_MIN && n4 <= N4_MAX);
149+
if (n4 < N4_MIN || n4 > N4_MAX)
150+
return GPS_TIME_UNKNOWN;
150151

151-
if (nt >= GLO_NT_0_FLOOR && nt <= GLO_NT_0_CEILING) {
152+
if (nt < GLO_NT_0_FLOOR)
153+
return GPS_TIME_UNKNOWN;
154+
else if (nt <= GLO_NT_0_CEILING) {
152155
j = 1;
153156
day_of_year = nt;
154157
}
@@ -165,7 +168,7 @@ gps_time_t glo_time2gps_time(u16 nt, u8 n4, s8 h, s8 m, s8 s)
165168
day_of_year = nt - LEAP_YEAR_DAYS - YEAR_DAYS * 2;
166169
}
167170
else
168-
assert(0 && "invalid nt");
171+
return GPS_TIME_UNKNOWN;
169172

170173
glo_year = 1996 + 4 * (n4 - 1) + (j - 1);
171174

tests/check_time.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,14 @@ START_TEST(test_glo_time2gps_time)
201201
/* GLO time 31st Dec 2011 12:12:12 */
202202
{.nt = 1461, .n4 = 4, .h = 12, .m = 12, .s = 12,
203203
.ret = {.wn = 1668, .tow = 551549}},
204+
{.nt = 0, .n4 = 4, .h = 12, .m = 12, .s = 12,
205+
.ret = GPS_TIME_UNKNOWN},
206+
{.nt = 1462, .n4 = 4, .h = 12, .m = 12, .s = 12,
207+
.ret = GPS_TIME_UNKNOWN},
208+
{.nt = 1461, .n4 = 0, .h = 12, .m = 12, .s = 12,
209+
.ret = GPS_TIME_UNKNOWN},
210+
{.nt = 1461, .n4 = 32, .h = 12, .m = 12, .s = 12,
211+
.ret = GPS_TIME_UNKNOWN},
204212
};
205213
for (size_t i = 0;
206214
i < sizeof(testcases) / sizeof(struct glo_time2gps_time_testcase);

0 commit comments

Comments
 (0)