| 1 | // Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.cxx . . $ `<file>/tests/test_send_frame.c`/ |
| 2 | // SPDX-License-Identifier: mit |
| 3 | |
| 4 | #include "amqp_private.h"cxx . . $ `<file>/librabbitmq/amqp_private.h`/ |
| 5 | #include <rabbitmq-c/amqp.h>cxx . . $ `<file>/include/rabbitmq-c/amqp.h`/ |
| 6 | #include <rabbitmq-c/framing.h>cxx . . $ `<file>/include/rabbitmq-c/framing.h`/ |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | |
| 12 | /* Regression test for GHSA-hfjv-vcp3-39wh: passing an oversized |
| 13 | * AMQP_FRAME_BODY to amqp_send_frame() must not overflow the outbound |
| 14 | * buffer. It should be rejected with AMQP_STATUS_BAD_AMQP_DATA. */ |
| 15 | static void test_oversized_body_frame_rejectedcxx . . $ test_oversized_body_frame_rejected(49f6e7a06ebc5aa8).(void) { |
| 16 | amqp_connection_state_tcxx . . $ amqp_connection_state_t# state = amqp_new_connectioncxx . . $ amqp_new_connection(79af3635f28998e8).(); |
| 17 | amqp_frame_tcxx . . $ amqp_frame_t# frame; |
| 18 | size_t body_len; |
| 19 | char *body; |
| 20 | int res; |
| 21 | |
| 22 | if (state == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 23 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "amqp_new_connection failed\n"); |
| 24 | abortcxx . . $ abort(8df32a62017fbbf6).(); |
| 25 | } |
| 26 | |
| 27 | /* The default outbound buffer is AMQP_DEFAULT_FRAME_SIZE bytes; use a |
| 28 | * body fragment that is larger than that buffer can hold. */ |
| 29 | body_len = state->outbound_buffercxx . . $ amqp_connection_state_t_#outbound_buffer..lencxx . . $ amqp_bytes_t_#len. + 1024; |
| 30 | body = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(body_len); |
| 31 | if (body == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 32 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "malloc failed\n"); |
| 33 | abortcxx . . $ abort(8df32a62017fbbf6).(); |
| 34 | } |
| 35 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(body, 'A', body_len); |
| 36 | |
| 37 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(&frame, 0, sizeof(frame)); |
| 38 | frame.frame_typecxx . . $ amqp_frame_t_#frame_type. = AMQP_FRAME_BODYcxx . . $ `include/rabbitmq-c/framing.h:22:9`!; |
| 39 | frame.channelcxx . . $ amqp_frame_t_#channel. = 1; |
| 40 | frame.payloadcxx . . $ amqp_frame_t_#payload..body_fragmentcxx . . $ amqp_frame_t_#$anonymous_type_2#body_fragment..bytescxx . . $ amqp_bytes_t_#bytes. = body; |
| 41 | frame.payloadcxx . . $ amqp_frame_t_#payload..body_fragmentcxx . . $ amqp_frame_t_#$anonymous_type_2#body_fragment..lencxx . . $ amqp_bytes_t_#len. = body_len; |
| 42 | |
| 43 | res = amqp_send_framecxx . . $ amqp_send_frame(ef503f58e2597482).(state, &frame); |
| 44 | if (res != AMQP_STATUS_BAD_AMQP_DATAcxx . . $ AMQP_STATUS_BAD_AMQP_DATA.) { |
| 45 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "expected AMQP_STATUS_BAD_AMQP_DATA (%d), got %d\n", |
| 46 | AMQP_STATUS_BAD_AMQP_DATAcxx . . $ AMQP_STATUS_BAD_AMQP_DATA., res); |
| 47 | abortcxx . . $ abort(8df32a62017fbbf6).(); |
| 48 | } |
| 49 | |
| 50 | freecxx . . $ free(5d22bdacc48458e8).(body); |
| 51 | amqp_destroy_connectioncxx . . $ amqp_destroy_connection(6448aa3e62633771).(state); |
| 52 | } |
| 53 | |
| 54 | int maincxx . . $ main(b126dc7c1de90089).(void) { |
| 55 | test_oversized_body_frame_rejectedcxx . . $ test_oversized_body_frame_rejected(49f6e7a06ebc5aa8).(); |
| 56 | return 0; |
| 57 | } |