1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-24 06:04:05 +03:00

Merge pull request #31044 from keszybz/uhttpd-alloca-print

Use macro wrapper instead of alloca in µhttp-utils
This commit is contained in:
Luca Boccassi 2024-01-22 22:03:08 +00:00 committed by GitHub
commit ad12e4be6d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 72 additions and 35 deletions

View File

@ -25,11 +25,13 @@ void microhttpd_logger(void *arg, const char *fmt, va_list ap) {
REENABLE_WARNING;
}
static int mhd_respond_internal(struct MHD_Connection *connection,
enum MHD_RequestTerminationCode code,
const char *buffer,
size_t size,
enum MHD_ResponseMemoryMode mode) {
int mhd_respond_internal(
struct MHD_Connection *connection,
enum MHD_RequestTerminationCode code,
const char *buffer,
size_t size,
enum MHD_ResponseMemoryMode mode) {
assert(connection);
_cleanup_(MHD_destroy_responsep) struct MHD_Response *response
@ -43,29 +45,16 @@ static int mhd_respond_internal(struct MHD_Connection *connection,
return MHD_queue_response(connection, code, response);
}
int mhd_respond(struct MHD_Connection *connection,
enum MHD_RequestTerminationCode code,
const char *message) {
const char *fmt;
fmt = strjoina(message, "\n");
return mhd_respond_internal(connection, code,
fmt, strlen(message) + 1,
MHD_RESPMEM_PERSISTENT);
}
int mhd_respond_oom(struct MHD_Connection *connection) {
return mhd_respond(connection, MHD_HTTP_SERVICE_UNAVAILABLE, "Out of memory.");
return mhd_respond(connection, MHD_HTTP_SERVICE_UNAVAILABLE, "Out of memory.");
}
int mhd_respondf(struct MHD_Connection *connection,
int error,
enum MHD_RequestTerminationCode code,
const char *format, ...) {
int mhd_respondf_internal(
struct MHD_Connection *connection,
int error,
enum MHD_RequestTerminationCode code,
const char *format, ...) {
const char *fmt;
char *m;
int r;
va_list ap;
@ -76,11 +65,8 @@ int mhd_respondf(struct MHD_Connection *connection,
if (error < 0)
error = -error;
errno = -error;
fmt = strjoina(format, "\n");
va_start(ap, format);
DISABLE_WARNING_FORMAT_NONLITERAL;
r = vasprintf(&m, fmt, ap);
REENABLE_WARNING;
r = vasprintf(&m, format, ap);
va_end(ap);
if (r < 0)

View File

@ -62,17 +62,34 @@ void microhttpd_logger(void *arg, const char *fmt, va_list ap) _printf_(2, 0);
/* respond_oom() must be usable with return, hence this form. */
#define respond_oom(connection) log_oom(), mhd_respond_oom(connection)
int mhd_respondf(struct MHD_Connection *connection,
int error,
enum MHD_RequestTerminationCode code,
const char *format, ...) _printf_(4,5);
int mhd_respond(struct MHD_Connection *connection,
int mhd_respond_internal(
struct MHD_Connection *connection,
enum MHD_RequestTerminationCode code,
const char *message);
const char *buffer,
size_t size,
enum MHD_ResponseMemoryMode mode);
#define mhd_respond(connection, code, message) \
mhd_respond_internal( \
connection, code, \
message "\n", \
strlen(message) + 1, \
MHD_RESPMEM_PERSISTENT)
int mhd_respond_oom(struct MHD_Connection *connection);
int mhd_respondf_internal(
struct MHD_Connection *connection,
int error,
enum MHD_RequestTerminationCode code,
const char *format, ...) _printf_(4,5);
#define mhd_respondf(connection, error, code, format, ...) \
mhd_respondf_internal( \
connection, error, code, \
format "\n", \
##__VA_ARGS__)
int check_permissions(struct MHD_Connection *connection, int *code, char **hostname);
/* Set gnutls internal logging function to a callback which uses our

View File

@ -142,3 +142,37 @@ curl -Lfsk https://localhost:19531/machine | jq
curl -Lfsk https://localhost:19531/fields/_TRANSPORT
kill "$GATEWAYD_PID"
# Test a couple of error scenarios
GATEWAYD_FILE="$(mktemp /tmp/test-gatewayd-XXX.journal)"
/usr/lib/systemd/systemd-journal-remote --output="$GATEWAYD_FILE" --getter="journalctl -n5 -o export"
systemd-run --unit="test-gatewayd.service" --socket-property="ListenStream=19531" \
/usr/lib/systemd/systemd-journal-gatewayd --file="$GATEWAYD_FILE"
# Call an unsupported endpoint together with some garbage data - gatewayd should not send garbage in return
# See: https://github.com/systemd/systemd/issues/9858
OUT="$(mktemp)"
for _ in {0..4}; do
curl --fail-with-body -d "plese process this🐱 $RANDOM" -L http://localhost:19531/upload | tee "$OUT"
(! grep '[^[:print:]]' "$OUT")
done
curl --fail-with-body --upload-file "$GATEWAYD_FILE" -L http://localhost:19531/upload | tee "$OUT"
(! grep '[^[:print:]]' "$OUT")
rm -rf "$OUT"
curl -Lfs http://localhost:19531/browse | grep -qF "<title>Journal</title>"
# Nuke the file behind the /browse endpoint
mv /usr/share/systemd/gatewayd/browse.html /usr/share/systemd/gatewayd/browse.html.bak
(! curl --fail-with-body -L http://localhost:19531/browse)
mv /usr/share/systemd/gatewayd/browse.html.bak /usr/share/systemd/gatewayd/browse.html
curl -Lfs http://localhost:19531/browse | grep -qF "<title>Journal</title>"
# Nuke the journal file
mv "$GATEWAYD_FILE" "$GATEWAYD_FILE.bak"
(! curl --fail-with-body -L http://localhost:19531/fields/_PID)
mv "$GATEWAYD_FILE.bak" "$GATEWAYD_FILE"
curl -Lfs http://localhost:19531/fields/_PID
systemctl stop test-gatewayd.{socket,service}
rm -f "$GATEWAYD_FILE"