1401 Commits

Author SHA1 Message Date
Azat Khuzhin
539f73e319 Fix leak in evbuffer_add_file() on empty files
Found by oss-fuzz, after coverage had been improved in google/oss-fuzz#11257
v2: adjust test
v3: fix for windows (_get_osfhandle() crashes when called on closed fd)
v4: fix for EVENT__DISABLE_MM_REPLACEMENT
2024-03-12 09:29:47 +01:00
tgolang
9c8860ec6c chore: remove repetitive words
Signed-off-by: tgolang <seekseat@aliyun.com>
2024-03-11 09:18:25 +01:00
Andy Pan
e66df92cfc
Accept SOCK_NONBLOCK/SOCK_CLOEXEC in type argument of socketpair (#1567)
Setting `SOCK_NONBLOCK` and `SOCK_CLOEXEC` in the `type` argument of `socketpair()` is widely supported across UNIX-like OS: Linux, *BSD, Solaris, etc., as is the `socket()`. This will conserve several extra system calls, we should use it where available.

### References

- [socketpair(2) on Linux](https://man7.org/linux/man-pages/man2/socketpair.2.html#HISTORY)
- [socketpair(2) on FreeBSD](https://man.freebsd.org/cgi/man.cgi?query=socketpair&sektion=2#DESCRIPTION)
- [socketpair(2) on DragonFly](https://man.dragonflybsd.org/?command=socketpair&section=2)
- [socketpair(2) on NetBSD](https://man.netbsd.org/socketpair.2#DESCRIPTION)
- [socketpair(2) on OpenBSD](https://man.openbsd.org/socketpair.2)
- [socketpair(3C) on Solaris](https://docs.oracle.com/cd/E88353_01/html/E37843/socketpair-3c.html)

Changelog:
- Set SOCK_NONBLOCK and SOCK_CLOEXEC in the type argument of socketpair
- Avoid EPROTOTYPE on macOS and OpenBSD
- Eliminate the warnings about unused variables
- Add some comments
2024-03-07 09:19:11 +01:00
Azat Khuzhin
e4b8732701 Fix integer-overflow in ev_token_bucket_cfg_new
Found by oss-fuzz, after coverage had been improved in https://github.com/google/oss-fuzz/pull/11257

v2: better check (found by CI for windows)
2024-03-03 16:25:08 +01:00
Azat Khuzhin
c4fb0f7603 Fix divide-by-zero in ev_token_bucket_get_tick_
Found by oss-fuzz, after coverage had been improved in https://github.com/google/oss-fuzz/pull/11257
2024-02-20 08:58:04 +01:00
Azat Khuzhin
0dbd3eb3c9 tests: add test for EV_SIGNAL with timeout
Refs: https://github.com/libevent/libevent-book/issues/12
2024-02-19 22:07:32 +01:00
Ramon Ortega de Voor
ed35b30feb Fix two compiler errors for unused variable and undefined function
In buffer.c a variable "flags" and a label "done" are defined but
never used if "EVENT__HAVEMMAP" is not defined.

The code does not work on platforms which do not provide
the function `socketpair()`. Introduce EVENT__HAVE_SOCKETPAIR flag
which determines if `socketpair()` or `evutil_ersatz_socketpair()`
is used.
2024-02-19 08:19:24 +01:00
Azat Khuzhin
87cdcc1868 http: change error for corrupted requests to 400 Bad Request
Previously it was 413 Request Entity Too Large, which was odd.
2024-02-18 17:02:45 +01:00
Azat Khuzhin
e60d039ddf tests: add a test for malformed chunks
v2: fix test for win32 (and it more correct in general)
2024-02-18 17:02:32 +01:00
Sam James
4c38de8cb3
Fix -Walloc-size (#1526)
Co-authored-by: Azat Khuzhin <azat@libevent.org>
2023-11-26 21:52:32 +01:00
Azat Khuzhin
c15ba75d18 Merge branch 'evbuffer_add_reference_with_offset' - #1513
* evbuffer_add_reference_with_offset:
  Add a comment for evbuffer_ref_cleanup_cb
  tests: simplify test_evbuffer_add_reference_with_offset
  Add function evbuffer_add_reference_with_offset()
2023-09-22 09:35:42 +02:00
Azat Khuzhin
d9780cf96a Fix -Wsingle-bit-bitfield-constant-conversion warning in clang 16
report:

    /src/le/libevent/sample/becat.c:304:29: warning: implicit truncation from 'int' to a one-bit wide bit-field changes value from 1 to -1 [-Wsingle-bit-bitfield-constant-conversion]
                            case 'k': o.extra.keep   = 1; break;
2023-09-21 22:04:17 +02:00
Azat Khuzhin
cc3a920073 tests: simplify test_evbuffer_add_reference_with_offset 2023-09-21 22:02:48 +02:00
MBeanwenshengming
648ec50e11 Add function evbuffer_add_reference_with_offset()
This is the same as evbuffer_add_reference(), but allows to specify
offset in the @data

v2: rename evbuffer_add_reference_misalign() to evbuffer_add_reference_with_offset()
2023-09-21 22:02:26 +02:00
Dmitry Ilyin
f39ad1c494
ws: replace evws_send with evws_send_text/evws_send_binary (ABI breakage) (#1500)
Replace evws_send with evws_send_text, and introduce new API -
evws_send_binary, that can be used to send binary frames.

But note, that this commit breaks the ABI compatibility, but it should be OK,
since there was only alpha release with evws_send, and nobody should rely on
this, and I hope nobody does (we decided to go this way to avoid supporting
deprecated API).
2023-08-31 21:38:41 +02:00
Azat Khuzhin
57d9eec641 Disable signalfd by default
signalfd may behave differently to sigaction/signal, so to avoid
breaking libevent users (like [1], [2]) disable it by default.

  [1]: https://github.com/tmux/tmux/pull/3621
  [2]: https://github.com/tmux/tmux/pull/3626

Also signalfd is not that perfect:
- you need to SIG_BLOCK the signal before
  - blocked signals are not reset on exec
  - blocked signals are allowed to coalesce - so in case of multiple
    signals sent you may get the signal only once (ok for most of the
    signals, but may be a problem for SIGCHLD, though you may call
    waitpid() in a loop or use pidfd)
- and also one implementation problem -
  sigprocmask is unspecified in a multithreaded process

Refs:
- https://lwn.net/Articles/415684/
- https://ldpreload.com/blog/signalfd-is-useless

Refs: https://github.com/libevent/libevent/issues/1460
Refs: #1342 (cc @dmantipov)
2023-07-13 21:21:09 +02:00
Vladislav Gusev
227510d577
Fix EVDNS_BASE_DISABLE_WHEN_INACTIVE (#1493)
I faced with strange problem: event loop doesn't exit after dns resolving with
`EVDNS_BASE_DISABLE_WHEN_INACTIVE`.

Stand:
- Ubuntu 22;
- libevent release-2.1.12-stable
- `resolve.conf` contains 2 nameservers;
- I use `evdns_base_new` with `EVDNS_BASE_DISABLE_WHEN_INACTIVE | EVDNS_BASE_INITIALIZE_NAMESERVERS` to avoid OS specific code.

After small investigation, look like events related with dns sockets added to
event_base before `evdns->disable_when_inactive` was initialized. `libevent`
did epoll_ctl(DEL) after resolving completed on the first socket, but the
second socket remained in the `epoll` interest list.
2023-07-13 21:20:33 +02:00
Krzysztof Dynowski
13366d2711 Test timer - synchronize clock before tv_timeout calculation. 2023-06-21 09:47:07 +02:00
Azat Khuzhin
fe610f2cdc Fix util/mm_calloc_enomem under FreeBSD
It looks like the compiler optimizes this call out with tt_assert():

    (gdb) disas /m test_event_calloc_enomem
    Dump of assembler code for function test_event_calloc_enomem:
       0x0000000000293bb0 <+0>:     push   %rbp
       0x0000000000293bb1 <+1>:     mov    %rsp,%rbp
       0x0000000000293bb4 <+4>:     call   0x29f510 <__error@plt>
       0x0000000000293bb9 <+9>:     movl   $0x0,(%rax)
       0x0000000000293bbf <+15>:    call   0x2990e0 <tinytest_set_test_failed_>
       0x0000000000293bc4 <+20>:    mov    $0x2168e4,%edi
       0x0000000000293bc9 <+25>:    mov    $0x220582,%esi
       0x0000000000293bce <+30>:    mov    $0x20d893,%edx
       0x0000000000293bd3 <+35>:    mov    $0x53a,%ecx
       0x0000000000293bd8 <+40>:    xor    %eax,%eax
       0x0000000000293bda <+42>:    call   0x29f3b0 <printf@plt>
       0x0000000000293bdf <+47>:    mov    $0x20da72,%edi
       0x0000000000293be4 <+52>:    mov    $0x20f731,%esi
       0x0000000000293be9 <+57>:    xor    %eax,%eax
       0x0000000000293beb <+59>:    pop    %rbp
       0x0000000000293bec <+60>:    jmp    0x29f3b0 <printf@plt>

While with tt_ptr_op() it does not:

    (gdb) disas /m test_event_calloc_enomem
    Dump of assembler code for function test_event_calloc_enomem:
       0x0000000000293bd0 <+0>:     push   %rbp
       0x0000000000293bd1 <+1>:     mov    %rsp,%rbp
       0x0000000000293bd4 <+4>:     push   %rbx
       0x0000000000293bd5 <+5>:     push   %rax
       0x0000000000293bd6 <+6>:     call   0x29f610 <__error@plt>
       0x0000000000293bdb <+11>:    movl   $0x0,(%rax)
       0x0000000000293be1 <+17>:    mov    $0xffffffffffffffff,%rdi
       0x0000000000293be8 <+24>:    mov    $0xffffffffffffffff,%rsi
       0x0000000000293bef <+31>:    call   0x2a01c0 <calloc@plt>
       0x0000000000293bf4 <+36>:    test   %rax,%rax
       0x0000000000293bf7 <+39>:    je     0x293c2b <test_event_calloc_enomem+91>
2023-05-14 22:35:53 +02:00
Azat Khuzhin
13f5552336 test: add missing include of arpa/inet.h for ntohs in regress_ws.c
This will fix one warning in FreeBSD
2023-05-14 22:31:02 +02:00
Azat Khuzhin
6eba967e1c Suppress -Wmacro-redefined for htonll/ntohll in OSX
OSX:

    test/regress_ws.c:61:9: warning: 'htonll' macro redefined [-Wmacro-redefined]
    #define htonll(x)    \
            ^
    /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/sys/_endian.h:141:9: note: previous definition is here
    #define htonll(x)       __DARWIN_OSSwapInt64(x)
            ^
    test/regress_ws.c:65:9: warning: 'ntohll' macro redefined [-Wmacro-redefined]
    #define ntohll(x) htonll(x)
            ^
    /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/sys/_endian.h:140:9: note: previous definition is here
    #define ntohll(x)       __DARWIN_OSSwapInt64(x)
            ^
2023-05-14 22:01:18 +02:00
Azat Khuzhin
5f1fc92b13 Fix -Wtautological-constant-out-of-range-compare in regress_http under OSX
compiler warning:

    test/regress_http.c:968:38: warning: result of comparison of constant 65536 with expression of type 'enum evhttp_cmd_type' is always true [-Wtautological-constant-out-of-range-compare]
            if (evhttp_request_get_command(req) != EVHTTP_REQ_CUSTOM) {
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^  ~~~~~~~~~~~~~~~~~
2023-05-14 21:55:43 +02:00
Azat Khuzhin
f04d90b121 Fix building with -Wstack-protector due to VLA and fobid them
It is not a thankless task to fix such issues on and on, let's just
prohibit this cases, and our build with -Werror on CI will show new
issues from now on.

Fixes: #1434
2023-04-04 22:17:14 +02:00
Zhipeng Xue
fa05966d74
test: fix potential null dereference in https_bind_ssl_bevcb (#1428)
Co-authored-by: Azat Khuzhin <a3at.mail@gmail.com>
2023-03-04 22:12:04 +01:00
Azat Khuzhin
1df2a5a69e test: enable allocator_may_return_null=1 for calloc with ENOMEM test 2023-02-12 21:55:40 +01:00
Azat Khuzhin
207ea62bf9 test: handle -v as --verbose for regress 2023-02-12 21:55:40 +01:00
Azat Khuzhin
0ea2058252 test: add del_wait/del_notify tests for windows
Test manually, since CI is too slow
2023-02-12 21:54:45 +01:00
Azat Khuzhin
61de8a07c5
test: fix leaks in bufferevent_pair_release_lock (#1413) 2023-02-12 15:47:16 +01:00
Azat Khuzhin
c257e16f5b test: fix debug locks in case new lock gots old address
Refs: #1407
2023-02-12 08:49:47 +01:00
Azat Khuzhin
fb900a284f test: suppress logs from the tests that produce them under normal circumstances 2023-02-12 08:39:20 +01:00
Azat Khuzhin
1201bb8529 test: fix TT_* flags values 2023-02-12 08:39:00 +01:00
Azat Khuzhin
35375101e7 Fixes some new warnings under clang-15
- -Wdeprecated-non-prototype

  /src/le/libevent/strlcpy.c:48:1: warning: a function definition without a prototype is deprecated in all versions of C and is not supported in C2x [-Wdeprecated-non-prototype]
  event_strlcpy_(dst, src, siz)

- -Wstrict-prototypes

  /src/le/libevent/evthread.c:82:70: warning: a function declaration without a prototype is deprecated in all versions of C [-Wstrict-prototypes]
  struct evthread_condition_callbacks *evthread_get_condition_callbacks()

- -Wunused-but-set-variable

  /src/le/libevent/test/regress_buffer.c:130:6: warning: variable 'n' set but not used [-Wunused-but-set-variable]
          int n = 0;
                                                                     ^
2023-01-27 08:58:51 +01:00
Daniel Kempenich
ebd7e8d793 Allow evdns_base_new to succeed with no nameservers configured
If resolv.conf has no nameservers, evdns_base_new can still succeed with
the default of using the name server from localhost matching the man
page documentation for resolv.conf.
2023-01-27 08:50:29 +01:00
Daniel Kempenich
a7fffb5c0f Replace magic numbers with consts for evdns_base_resolv_conf_parse() errors 2023-01-27 08:47:01 +01:00
Dmitry Antipov
1af745d033 signal: new signal handling backend based on signalfd
Linux-specific signal handling backend based on signalfd(2)
system call, and public function event_base_get_signal_method()
to obtain an underlying kernel signal handling mechanism.

Signed-off-by: Dmitry Antipov <dantipov@cloudlinux.com>
2022-11-12 21:14:48 +01:00
Leon George
3ec3b469b8
ws: fix compile error on centos 7 - very old compiler (#1359)
* http: fix typo

* ws: fix comile error

On CentOS:

  CC       ws.lo
ws.c: In function 'get_ws_frame':
ws.c:244:3: error: 'for' loop initial declarations are only allowed in C99 mode
   for (int i = 0; i < payload_len; i++) {
   ^
ws.c:244:3: note: use option -std=c99 or -std=gnu99 to compile your code
2022-10-23 14:47:23 +03:00
Dmitry Ilyin
c2ecb4acb5 Add locks for server WS, fixes #1357 2022-10-12 14:13:44 +03:00
Dmitry Ilyin
aa163a4f29 Fix memleak in regress tests 2022-10-04 21:42:30 +03:00
Dmitry Ilyin
88317a4ef8 Add helpers and all regress tests are passed 2022-10-04 17:49:22 +03:00
zhenhaonong
f8bb9d8484 Fix socketpair failure when temporary directory has non-latin character 2022-09-26 21:43:21 +03:00
Dmitry Ilyin
e8313084f9
Add minimal WebSocket server implementation for evhttp (#1322)
This adds few functions to use evhttp-based webserver to handle incoming
WebSockets connections. We've tried to use both libevent and libwebsockets in
our application, but found that we need to have different ports at the same
time to handle standard HTTP and WebSockets traffic. This change can help to
stick only with libevent library.

Implementation was inspired by modified Libevent source code in ipush project
[1].

  [1]: https://github.com/sqfasd/ipush/tree/master/deps/libevent-2.0.21-stable

Also, WebSocket-based chat server was added as a sample.
2022-09-12 22:16:56 +03:00
Azat Khuzhin
bb57cea387 test: fix util/getaddrinfo for netbsd (v2)
Fixes: c198b0ce ("test: fix util/getaddrinfo for netbsd")
Fixes: #1316
2022-08-14 09:56:50 +02:00
Azat Khuzhin
77a9b60e47
Merge pull request #1315 from yogo1212/http_per_socket_bebcb
In it's current form, libevent requires multiple struct evhttp objects to be created in order to enable listening on sockets with more than one type of encryption.

This change allows specifying per-socket how the associated bufferevents should be created.
Thus, it becomes possible to have multiple listening sockets with different encryption parameters using only one evttp.
2022-08-14 00:46:48 +02:00
Azat Khuzhin
c198b0ceb3 test: fix util/getaddrinfo for netbsd
Fixes: #1316
2022-08-13 20:48:49 +02:00
Leon M. George
1bdc91350e http: allow setting bevcb per socket
Co-authored-by: Azat Khuzhin <azat@libevent.org>
v2: remove handling of HTTP_BIND_IPV6
2022-08-13 20:12:18 +02:00
Azat Khuzhin
a4cdc3c5e8 test: allow to run init_ssl() multiple times 2022-08-13 20:12:18 +02:00
Azat Khuzhin
4ca417afa4 test: add a comment for init_ssl() about suppressions for LSan 2022-08-13 20:12:08 +02:00
Azat Khuzhin
33fb0e358a test: fix unused variable in rand test (catched by newer clang) 2022-07-12 09:54:36 +03:00
Azat Khuzhin
213a822aa7 test: increase timeout significantly in dns/getaddrinfo_cancel_stress (for TSan)
Fixes: #1304
2022-07-11 22:53:57 +03:00
Azat Khuzhin
5ff98dc1f3 evdns: accept domains up to 254 long (previosly only 63 long was accepted)
Previously evdns was using HOST_NAME_MAX, and define it to 255 *only* if
it not set, however it does set on linux:

    $ egrep -r define.*HOST_NAME_MAX /usr/include/bits
    /usr/include/bits/local_lim.h:#define HOST_NAME_MAX             64
    /usr/include/bits/posix1_lim.h:#define _POSIX_HOST_NAME_MAX     255
    /usr/include/bits/confname.h:#define _SC_HOST_NAME_MAX          _SC_HOST_NAME_MAX

But 64 should be the limit of the host component, not for the whole
hostname, as also noted by @ploxiln

So use our own EVDNS_NAME_MAX const, which is set to 255.

Fixes: #1280
2022-07-10 18:10:35 +03:00