If server_stop() is called before any client connects, the accept()
loop would spin forever since kill_recv was never checked, causing
pthread_join() to block indefinitely.
Add kill_recv check at the top of the accept() polling loop so the
thread can exit cleanly when server_stop() is called during the
pre-connection phase.
The recv() drain loop on listen_socket was incorrect - listen sockets
don't have data to read. This caused Valgrind to report 'points to
unaddressable byte(s)' errors during functional tests.
Replaced magic number 2 with SHUT_RDWR for clarity.
The check for logready was done BEFORE acquiring the lock, causing a race
condition with log_close(). If log_close() runs between the check and
the lock acquisition, the log file would be closed but log_println would
still try to write to it (use-after-free).
Now the logready/logp check is inside the critical section.
The 'stream' GString was not freed before returning from read_stream()
in two places:
1. When kill_recv is TRUE at the start of the loop
2. At the end of the function after the loop exits
Added g_string_free(stream, TRUE) before both return statements.
Valgrind reports:
Syscall param socketcall.recvfrom(buf) points to unaddressable byte(s)
at recv (recv.c:28)
by xmppclient_end_session (xmppclient.c:57)
Address 0x0 is not stack'd, malloc'd or (recently) free'd
The recv() call was passing NULL as buffer, which is invalid.
Now using a local char variable as discard buffer.
The XMPPStanza objects created by stanza_parse() in verify_any() and
verify_last() functions were never freed, causing memory leaks.
This fix adds stanza_free(stanza) calls before the return statements
to properly release the allocated stanza resources.
The GTimer objects created in verify_any() and verify_last() functions
were never freed, causing memory leaks detected by Valgrind.
This fix adds g_timer_destroy(timer) calls after the while loops to
properly release the allocated timer resources.
This PR fixes critical issues with XMPP stream parsing and adds a new helper function for MUC testing.
## Changes
### 1. Fix XMPP stream parsing for multiple stanzas
The XML parser was not correctly handling multiple consecutive stanzas in the stream. Fixed depth tracking to properly detect stanza boundaries.
### 2. Fix XMPP stream parsing depth tracking
Added proper depth tracking for nested XML elements to correctly identify when a complete stanza has been received.
### 3. Remove debug logging that interfered with expect tests
Removed verbose debug output that was causing expect-based functional tests to fail due to unexpected output in the stream.
### 4. Add `stbbr_for_presence_to` for MUC join presence matching
New helper function that registers a canned response for presence stanzas based on the `to` attribute. This is essential for testing MUC (Multi-User Chat) join flows where the response must be triggered by presence to a specific room JID.
**API:**
```c
void stbbr_for_presence_to(const char *to, const char *response);
Reviewed-on: jabber.developer2/stabber#1
Co-authored-by: jabber.developer2 <jabber.developer2@jabber.space>
Co-committed-by: jabber.developer2 <jabber.developer2@jabber.space>
Profanity Travis CI showed that stabber doesn't build anymore on Debian
Testing, Arch Linux and Fedora.
It appears all these systems have updated to libmicrohttpd 0.9.71
recently.
Earlier versions build fine.
See release announcement: https://lists.gnu.org/archive/html/libmicrohttpd/2020-06/msg00013.html
```
Furthermore, the release introduces an 'enum MHD_Result' instead of
for certain API misuse bugs by providing better types (not everything is
an 'int'). While this does NOT change the binary API, this change
_will_ cause compiler warnings for all legacy code -- until 'int' is
replaced with 'enum MHD_Result'.
```