Skip to content

Commit f18010d

Browse files
committed
fix: apply suggestions from ai code review
1 parent 02d9919 commit f18010d

2 files changed

Lines changed: 48 additions & 20 deletions

File tree

base/hbase.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ time_t hv_parse_time(const char* str) {
450450
}
451451

452452
int hv_parse_url(hurl_t* stURL, const char* strURL) {
453+
int ret = 0;
453454
if (stURL == NULL || strURL == NULL) return -1;
454455
memset(stURL, 0, sizeof(hurl_t));
455456
const char* begin = strURL;
@@ -504,14 +505,18 @@ int hv_parse_url(hurl_t* stURL, const char* strURL) {
504505
unsigned int parsed_port = 0;
505506
for (unsigned short i = 1; i <= stURL->fields[HV_URL_PORT].len; ++i) {
506507
if (port[i] < '0' || port[i] > '9') {
507-
return -2;
508+
ret = -2;
509+
break;
508510
}
509511
parsed_port = parsed_port * 10 + (port[i] - '0');
510512
if (parsed_port > 65535) {
511-
return -3;
513+
ret = -3;
514+
break;
512515
}
513516
}
514-
stURL->port = (unsigned short)parsed_port;
517+
if (ret == 0) {
518+
stURL->port = (unsigned short)parsed_port;
519+
}
515520
} else {
516521
port = ep;
517522
// set default port
@@ -526,25 +531,25 @@ int hv_parse_url(hurl_t* stURL, const char* strURL) {
526531
stURL->fields[HV_URL_HOST].off = host - begin;
527532
stURL->fields[HV_URL_HOST].len = port - host;
528533
}
529-
if (ep == end) return 0;
534+
if (ep == end) return ret;
530535
// /path
531536
sp = ep;
532537
ep = strchr(sp, '?');
533538
if (ep == NULL) ep = end;
534539
stURL->fields[HV_URL_PATH].off = sp - begin;
535540
stURL->fields[HV_URL_PATH].len = ep - sp;
536-
if (ep == end) return 0;
541+
if (ep == end) return ret;
537542
// ?query
538543
sp = ep + 1;
539544
ep = strchr(sp, '#');
540545
if (ep == NULL) ep = end;
541546
stURL->fields[HV_URL_QUERY].off = sp - begin;
542547
stURL->fields[HV_URL_QUERY].len = ep - sp;
543-
if (ep == end) return 0;
548+
if (ep == end) return ret;
544549
// #fragment
545550
sp = ep + 1;
546551
ep = end;
547552
stURL->fields[HV_URL_FRAGMENT].off = sp - begin;
548553
stURL->fields[HV_URL_FRAGMENT].len = ep - sp;
549-
return 0;
554+
return ret;
550555
}

base/hlog.c

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,41 @@
2929
//#include "htime.h"
3030
#define SECONDS_PER_HOUR 3600
3131
#define SECONDS_PER_DAY 86400 // 24*3600
32-
#define SECONDS_PER_WEEK 604800 // 7*24*3600;
32+
#define SECONDS_PER_WEEK 604800 // 7*24*3600
33+
34+
static inline struct tm* hv_localtime_r(time_t ts, struct tm* tm) {
35+
#ifdef _WIN32
36+
localtime_s(tm, &ts);
37+
#else
38+
tm = localtime_r(&ts, tm);
39+
#endif
40+
return tm;
41+
}
42+
43+
static inline struct tm* hv_gmtime_r(time_t ts, struct tm* tm) {
44+
#ifdef _WIN32
45+
gmtime_s(tm, &ts);
46+
#else
47+
tm = gmtime_r(&ts, tm);
48+
#endif
49+
return tm;
50+
}
3351

3452
static int s_gmtoff = 28800; // 8*3600
3553
static void init_gmtoff() {
3654
time_t ts = time(NULL);
37-
struct tm* local_tm = localtime(&ts);
38-
struct tm* gmt_tm = gmtime(&ts);
39-
s_gmtoff = (local_tm->tm_hour - gmt_tm->tm_hour) * 3600 +
40-
(local_tm->tm_min - gmt_tm->tm_min) * 60 +
41-
(local_tm->tm_sec - gmt_tm->tm_sec);
42-
43-
if (local_tm->tm_yday > gmt_tm->tm_yday) {
55+
struct tm local_tm, gmt_tm;
56+
memset(&local_tm, 0, sizeof(local_tm));
57+
memset(&gmt_tm, 0, sizeof(gmt_tm));
58+
hv_localtime_r(ts, &local_tm);
59+
hv_gmtime_r(ts, &gmt_tm);
60+
s_gmtoff = (local_tm.tm_hour - gmt_tm.tm_hour) * 3600 +
61+
(local_tm.tm_min - gmt_tm.tm_min) * 60 +
62+
(local_tm.tm_sec - gmt_tm.tm_sec);
63+
64+
if (local_tm.tm_yday > gmt_tm.tm_yday) {
4465
s_gmtoff += SECONDS_PER_DAY;
45-
} else if (local_tm->tm_yday < gmt_tm->tm_yday) {
66+
} else if (local_tm.tm_yday < gmt_tm.tm_yday) {
4667
s_gmtoff -= SECONDS_PER_DAY;
4768
}
4869
}
@@ -222,12 +243,14 @@ const char* logger_get_cur_file(logger_t* logger) {
222243
}
223244

224245
static void logfile_name(const char* filepath, time_t ts, char* buf, int len) {
225-
struct tm* tm = localtime(&ts);
246+
struct tm tm;
247+
memset(&tm, 0, sizeof(tm));
248+
hv_localtime_r(ts, &tm);
226249
snprintf(buf, len, "%s.%04d%02d%02d.log",
227250
filepath,
228-
tm->tm_year+1900,
229-
tm->tm_mon+1,
230-
tm->tm_mday);
251+
tm.tm_year+1900,
252+
tm.tm_mon+1,
253+
tm.tm_mday);
231254
}
232255

233256
static void logfile_truncate(logger_t* logger) {

0 commit comments

Comments
 (0)