Skip to content

Commit 303f50c

Browse files
authored
Apply suggestions from ai code review (#837)
* fix: apply suggestions for base dir from ai code review * fix: apply suggestions for cpputil dir from ai code review * fix: apply suggestions for ssl dir from ai code review * fix: apply suggestions for event dir from ai code review
1 parent 5da5fc2 commit 303f50c

33 files changed

Lines changed: 522 additions & 243 deletions

base/hbase.c

Lines changed: 18 additions & 5 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;
@@ -501,8 +502,20 @@ int hv_parse_url(hurl_t* stURL, const char* strURL) {
501502
stURL->fields[HV_URL_PORT].off = port + 1 - begin;
502503
stURL->fields[HV_URL_PORT].len = ep - port - 1;
503504
// atoi
505+
unsigned int parsed_port = 0;
504506
for (unsigned short i = 1; i <= stURL->fields[HV_URL_PORT].len; ++i) {
505-
stURL->port = stURL->port * 10 + (port[i] - '0');
507+
if (port[i] < '0' || port[i] > '9') {
508+
ret = -2;
509+
break;
510+
}
511+
parsed_port = parsed_port * 10 + (port[i] - '0');
512+
if (parsed_port > 65535) {
513+
ret = -3;
514+
break;
515+
}
516+
}
517+
if (ret == 0) {
518+
stURL->port = (unsigned short)parsed_port;
506519
}
507520
} else {
508521
port = ep;
@@ -518,25 +531,25 @@ int hv_parse_url(hurl_t* stURL, const char* strURL) {
518531
stURL->fields[HV_URL_HOST].off = host - begin;
519532
stURL->fields[HV_URL_HOST].len = port - host;
520533
}
521-
if (ep == end) return 0;
534+
if (ep == end) return ret;
522535
// /path
523536
sp = ep;
524537
ep = strchr(sp, '?');
525538
if (ep == NULL) ep = end;
526539
stURL->fields[HV_URL_PATH].off = sp - begin;
527540
stURL->fields[HV_URL_PATH].len = ep - sp;
528-
if (ep == end) return 0;
541+
if (ep == end) return ret;
529542
// ?query
530543
sp = ep + 1;
531544
ep = strchr(sp, '#');
532545
if (ep == NULL) ep = end;
533546
stURL->fields[HV_URL_QUERY].off = sp - begin;
534547
stURL->fields[HV_URL_QUERY].len = ep - sp;
535-
if (ep == end) return 0;
548+
if (ep == end) return ret;
536549
// #fragment
537550
sp = ep + 1;
538551
ep = end;
539552
stURL->fields[HV_URL_FRAGMENT].off = sp - begin;
540553
stURL->fields[HV_URL_FRAGMENT].len = ep - sp;
541-
return 0;
554+
return ret;
542555
}

base/hbase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ HV_EXPORT bool hv_wildcard_match(const char* str, const char* pattern);
7070
HV_EXPORT char* hv_strncpy(char* dest, const char* src, size_t n);
7171

7272
// strncat n = sizeof(dest_buf)-1-strlen(dest)
73-
// hv_strncpy n = sizeof(dest_buf)
73+
// hv_strncat n = sizeof(dest_buf)
7474
HV_EXPORT char* hv_strncat(char* dest, const char* src, size_t n);
7575

7676
#if !HAVE_STRLCPY

base/hdef.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@
167167

168168
#ifndef MAKE_FOURCC
169169
#define MAKE_FOURCC(a, b, c, d) \
170-
( ((uint32)d) | ( ((uint32)c) << 8 ) | ( ((uint32)b) << 16 ) | ( ((uint32)a) << 24 ) )
170+
( ((uint32_t)d) | ( ((uint32_t)c) << 8 ) | ( ((uint32_t)b) << 16 ) | ( ((uint32_t)a) << 24 ) )
171171
#endif
172172

173173
#ifndef MAX

base/hlog.c

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,44 @@
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
53+
static void init_gmtoff() {
54+
time_t ts = time(NULL);
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) {
65+
s_gmtoff += SECONDS_PER_DAY;
66+
} else if (local_tm.tm_yday < gmt_tm.tm_yday) {
67+
s_gmtoff -= SECONDS_PER_DAY;
68+
}
69+
}
3570

3671
struct logger_s {
3772
logger_handler handler;
@@ -79,13 +114,7 @@ static void logger_init(logger_t* logger) {
79114
}
80115

81116
logger_t* logger_create() {
82-
// init gmtoff here
83-
time_t ts = time(NULL);
84-
struct tm* local_tm = localtime(&ts);
85-
int local_hour = local_tm->tm_hour;
86-
struct tm* gmt_tm = gmtime(&ts);
87-
int gmt_hour = gmt_tm->tm_hour;
88-
s_gmtoff = (local_hour - gmt_hour) * SECONDS_PER_HOUR;
117+
init_gmtoff();
89118

90119
logger_t* logger = (logger_t*)malloc(sizeof(logger_t));
91120
logger_init(logger);
@@ -214,12 +243,14 @@ const char* logger_get_cur_file(logger_t* logger) {
214243
}
215244

216245
static void logfile_name(const char* filepath, time_t ts, char* buf, int len) {
217-
struct tm* tm = localtime(&ts);
246+
struct tm tm;
247+
memset(&tm, 0, sizeof(tm));
248+
hv_localtime_r(ts, &tm);
218249
snprintf(buf, len, "%s.%04d%02d%02d.log",
219250
filepath,
220-
tm->tm_year+1900,
221-
tm->tm_mon+1,
222-
tm->tm_mday);
251+
tm.tm_year+1900,
252+
tm.tm_mon+1,
253+
tm.tm_mday);
223254
}
224255

225256
static void logfile_truncate(logger_t* logger) {
@@ -375,17 +406,17 @@ int logger_print(logger_t* logger, int level, const char* fmt, ...) {
375406
us = tm.wMilliseconds * 1000;
376407
#else
377408
struct timeval tv;
378-
struct tm* tm = NULL;
379409
gettimeofday(&tv, NULL);
380-
time_t tt = tv.tv_sec;
381-
struct tm tm_buf;
382-
tm = localtime_r(&tt, &tm_buf);
383-
year = tm->tm_year + 1900;
384-
month = tm->tm_mon + 1;
385-
day = tm->tm_mday;
386-
hour = tm->tm_hour;
387-
min = tm->tm_min;
388-
sec = tm->tm_sec;
410+
time_t ts = tv.tv_sec;
411+
struct tm tm;
412+
memset(&tm, 0, sizeof(tm));
413+
localtime_r(&ts, &tm);
414+
year = tm.tm_year + 1900;
415+
month = tm.tm_mon + 1;
416+
day = tm.tm_mday;
417+
hour = tm.tm_hour;
418+
min = tm.tm_min;
419+
sec = tm.tm_sec;
389420
us = tv.tv_usec;
390421
#endif
391422

base/hmath.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ static inline int asn1_encode(long long value, unsigned char* buf) {
9292
*p = (unsigned char)value;
9393
return 3;
9494
}
95-
else if (value < 16777126)
95+
else if (value < 16777216)
9696
{
9797
*p = 0x83;
9898
p++;

base/htime.c

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,26 @@ datetime_t datetime_now() {
7373
}
7474

7575
datetime_t datetime_localtime(time_t seconds) {
76-
struct tm* tm = localtime(&seconds);
76+
struct tm tm;
77+
memset(&tm, 0, sizeof(tm));
78+
hv_localtime_r(seconds, &tm);
7779
datetime_t dt;
78-
dt.year = tm->tm_year + 1900;
79-
dt.month = tm->tm_mon + 1;
80-
dt.day = tm->tm_mday;
81-
dt.hour = tm->tm_hour;
82-
dt.min = tm->tm_min;
83-
dt.sec = tm->tm_sec;
80+
dt.year = tm.tm_year + 1900;
81+
dt.month = tm.tm_mon + 1;
82+
dt.day = tm.tm_mday;
83+
dt.hour = tm.tm_hour;
84+
dt.min = tm.tm_min;
85+
dt.sec = tm.tm_sec;
86+
dt.ms = 0;
8487
return dt;
8588
}
8689

8790
time_t datetime_mktime(datetime_t* dt) {
8891
struct tm tm;
8992
time_t ts;
9093
time(&ts);
91-
struct tm* ptm = localtime(&ts);
92-
memcpy(&tm, ptm, sizeof(struct tm));
94+
memset(&tm, 0, sizeof(tm));
95+
hv_localtime_r(ts, &tm);
9396
tm.tm_year = dt->year - 1900;
9497
tm.tm_mon = dt->month - 1;
9598
tm.tm_mday = dt->day;
@@ -171,12 +174,14 @@ char* datetime_fmt_iso(datetime_t* dt, char* buf) {
171174
}
172175

173176
char* gmtime_fmt(time_t time, char* buf) {
174-
struct tm* tm = gmtime(&time);
175-
//strftime(buf, GMTIME_FMT_BUFLEN, "%a, %d %b %Y %H:%M:%S GMT", tm);
177+
struct tm tm;
178+
memset(&tm, 0, sizeof(tm));
179+
hv_gmtime_r(time, &tm);
180+
//strftime(buf, GMTIME_FMT_BUFLEN, "%a, %d %b %Y %H:%M:%S GMT", &tm);
176181
sprintf(buf, GMTIME_FMT,
177-
s_weekdays[tm->tm_wday],
178-
tm->tm_mday, s_months[tm->tm_mon], tm->tm_year + 1900,
179-
tm->tm_hour, tm->tm_min, tm->tm_sec);
182+
s_weekdays[tm.tm_wday],
183+
tm.tm_mday, s_months[tm.tm_mon], tm.tm_year + 1900,
184+
tm.tm_hour, tm.tm_min, tm.tm_sec);
180185
return buf;
181186
}
182187

@@ -228,7 +233,8 @@ time_t cron_next_timeout(int minute, int hour, int day, int week, int month) {
228233
struct tm tm;
229234
time_t tt;
230235
time(&tt);
231-
tm = *localtime(&tt);
236+
memset(&tm, 0, sizeof(tm));
237+
hv_localtime_r(tt, &tm);
232238
time_t tt_round = 0;
233239

234240
tm.tm_sec = 0;

base/htime.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,24 @@ HV_INLINE int gettimeofday(struct timeval *tv, struct timezone *tz) {
5353
}
5454
#endif
5555

56+
HV_INLINE struct tm* hv_localtime_r(time_t ts, struct tm* tm) {
57+
#ifdef OS_WIN
58+
localtime_s(tm, &ts);
59+
#else
60+
tm = localtime_r(&ts, tm);
61+
#endif
62+
return tm;
63+
}
64+
65+
HV_INLINE struct tm* hv_gmtime_r(time_t ts, struct tm* tm) {
66+
#ifdef OS_WIN
67+
gmtime_s(tm, &ts);
68+
#else
69+
tm = gmtime_r(&ts, tm);
70+
#endif
71+
return tm;
72+
}
73+
5674
HV_EXPORT unsigned int gettick_ms();
5775
HV_INLINE unsigned long long gettimeofday_ms() {
5876
struct timeval tv;

base/queue.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ static inline void qtype##_cleanup(qtype* p) {\
7070
p->_offset = p->size = p->maxsize = 0;\
7171
}\
7272
\
73+
static inline void qtype##_realign(qtype* p) {\
74+
if (p->size == 0) {\
75+
p->_offset = 0;\
76+
}\
77+
else if (p->_offset > 0) {\
78+
memmove(p->ptr, p->ptr + p->_offset, sizeof(type) * p->size);\
79+
p->_offset = 0;\
80+
}\
81+
}\
82+
\
7383
static inline void qtype##_resize(qtype* p, int maxsize) {\
7484
if (maxsize == 0) maxsize = QUEUE_INIT_SIZE;\
7585
p->ptr = (type*)hv_realloc(p->ptr, sizeof(type) * maxsize, sizeof(type) * p->maxsize);\
@@ -85,8 +95,7 @@ static inline void qtype##_push_back(qtype* p, type* elem) {\
8595
qtype##_double_resize(p);\
8696
}\
8797
else if (p->_offset + p->size == p->maxsize) {\
88-
memmove(p->ptr, p->ptr + p->_offset, sizeof(type) * p->size);\
89-
p->_offset = 0;\
98+
qtype##_realign(p);\
9099
}\
91100
p->ptr[p->_offset + p->size] = *elem;\
92101
p->size++;\

0 commit comments

Comments
 (0)