Skip to content

Commit e8363f6

Browse files
committed
Add tests [5] --filter=[core] --asan
1 parent fc5977a commit e8363f6

File tree

6 files changed

+39
-16
lines changed

6 files changed

+39
-16
lines changed

core-tests/src/core/base.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ TEST(base, set_task_tmpdir) {
189189
ASSERT_FALSE(swoole_set_task_tmpdir("aaa"));
190190

191191
size_t length = SW_TASK_TMP_PATH_SIZE + 1;
192-
char too_long_dir[length] = {};
192+
char too_long_dir[length + 1] = {};
193193
swoole_random_string(too_long_dir + 1, length - 1);
194194
too_long_dir[0] = '/';
195195
ASSERT_FALSE(swoole_set_task_tmpdir(too_long_dir));

core-tests/src/coroutine/system.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,24 @@ TEST(coroutine_system, wait_event_writable) {
246246
});
247247
}
248248

249+
TEST(coroutine_system, wait_event_fail) {
250+
test::coroutine::run([&](void *arg) {
251+
ASSERT_EQ(System::wait_event(9999, 0, 1), SW_ERR);
252+
ASSERT_ERREQ(EINVAL);
253+
254+
ASSERT_EQ(System::wait_event(fileno(stdin), SW_EVENT_READ, 0), SW_ERR);
255+
ASSERT_ERREQ(ETIMEDOUT);
256+
257+
ASSERT_EQ(System::wait_event(fileno(stdout), SW_EVENT_WRITE, 0), SW_EVENT_WRITE);
258+
259+
ASSERT_EQ(System::wait_event(9999, SW_EVENT_WRITE, 0), -1);
260+
ASSERT_ERREQ(EBADF);
261+
262+
ASSERT_EQ(System::wait_event(9999, SW_EVENT_WRITE, 1.0), -1);
263+
ASSERT_ERREQ(EBADF);
264+
});
265+
}
266+
249267
TEST(coroutine_system, swoole_stream_select) {
250268
UnixSocket p(true, SOCK_STREAM);
251269
std::unordered_map<int, swoole::coroutine::PollSocket> fds;

ext-src/swoole_http_client_coro.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,13 +1152,13 @@ bool Client::send_request() {
11521152
// ============ multipart/form-data ============
11531153
if ((has_upload_files = (php_swoole_array_length_safe(zupload_files) > 0))) {
11541154
char header_buf[2048];
1155-
char boundary_str[SW_HTTP_CLIENT_BOUNDARY_TOTAL_SIZE];
1155+
char boundary_str[SW_HTTP_CLIENT_BOUNDARY_TOTAL_SIZE + 1];
11561156
int n;
11571157

11581158
// ============ content-type ============
11591159
memcpy(boundary_str, SW_HTTP_CLIENT_BOUNDARY_PREKEY, sizeof(SW_HTTP_CLIENT_BOUNDARY_PREKEY) - 1);
11601160
swoole_random_string(boundary_str + sizeof(SW_HTTP_CLIENT_BOUNDARY_PREKEY) - 1,
1161-
sizeof(boundary_str) - sizeof(SW_HTTP_CLIENT_BOUNDARY_PREKEY));
1161+
sizeof(boundary_str) - sizeof(SW_HTTP_CLIENT_BOUNDARY_PREKEY) - 1);
11621162
n = sw_snprintf(header_buf,
11631163
sizeof(header_buf),
11641164
"Content-Type: multipart/form-data; boundary=%.*s\r\n",

include/swoole.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,12 @@ typedef unsigned char uchar;
491491
#define swoole_tolower(c) (uchar)((c >= 'A' && c <= 'Z') ? (c | 0x20) : c)
492492
#define swoole_toupper(c) (uchar)((c >= 'a' && c <= 'z') ? (c & ~0x20) : c)
493493

494-
void swoole_random_string(char *buf, size_t size);
495-
void swoole_random_string(std::string &str, size_t size);
494+
/**
495+
* This function appends a '\0' at the end of the string,
496+
* so the allocated memory buffer must be len + 1.
497+
*/
498+
void swoole_random_string(char *buf, size_t len);
499+
void swoole_random_string(std::string &str, size_t len);
496500
uint64_t swoole_random_int();
497501
size_t swoole_random_bytes(char *buf, size_t size);
498502

src/core/base.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -797,17 +797,17 @@ static constexpr char characters[] = {
797797
'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
798798
};
799799

800-
void swoole_random_string(char *buf, size_t size) {
800+
void swoole_random_string(char *buf, size_t len) {
801801
size_t i = 0;
802-
for (; i < size; i++) {
802+
for (; i < len; i++) {
803803
buf[i] = characters[swoole_rand(0, sizeof(characters) - 1)];
804804
}
805805
buf[i] = '\0';
806806
}
807807

808-
void swoole_random_string(std::string &str, size_t size) {
808+
void swoole_random_string(std::string &str, size_t len) {
809809
size_t i = 0;
810-
for (; i < size; i++) {
810+
for (; i < len; i++) {
811811
str.append(1, characters[swoole_rand(0, sizeof(characters) - 1)]);
812812
}
813813
}

src/coroutine/system.cc

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -546,26 +546,27 @@ static int event_waiter_error_callback(Reactor *reactor, Event *event) {
546546
* @errror: errno & swoole_get_last_error()
547547
*/
548548
int System::wait_event(int fd, int events, double timeout) {
549-
events &= SW_EVENT_READ | SW_EVENT_WRITE;
550549
if (events == 0) {
551550
swoole_set_last_error(EINVAL);
552-
return 0;
551+
return -1;
553552
}
554553

555554
if (timeout == 0) {
556-
struct pollfd pfd;
555+
pollfd pfd;
557556
pfd.fd = fd;
558557
pfd.events = translate_events_to_poll(events);
559558
pfd.revents = 0;
560559

561560
int retval = ::poll(&pfd, 1, 0);
562561
if (retval == 1) {
562+
if (pfd.revents & POLLNVAL) {
563+
swoole_set_last_error(EBADF);
564+
return -1;
565+
}
563566
return translate_events_from_poll(pfd.revents);
564567
}
565-
if (retval < 0) {
566-
swoole_set_last_error(errno);
567-
}
568-
return 0;
568+
swoole_set_last_error(retval < 0 ? errno : ETIMEDOUT);
569+
return -1;
569570
}
570571

571572
EventWaiter waiter(fd, events, timeout);

0 commit comments

Comments
 (0)