Debian Codegraph @

test/rist/unit/test_url_udp_prefix_parse.c

raw symbols SCIP
1 /* librist. SPDX-License-Identifier: BSD-2-Clause
2 *
3 * udp:// scheme-prefix parsing exercised through the public
4 * rist_parse_udp_address2 API. The prefix[] field is 16 bytes; the parser
5 * must not underflow its copy length on a leading '/' nor write the NUL
6 * terminator past the buffer on an over-long scheme. Runs under ASan to
7 * catch the out-of-bounds write directly. No cmocka: links the public API
8 * only, so it runs anywhere the library does. */
9
10 #include "librist/librist.h"
11
12 #include <stdio.h>
13 #include <string.h>
14
15 static int expect_prefix(const char *url, const char *want_prefix, int want_rtp)
16 {
17 struct rist_udp_config *cfg = NULL;
18 int ret = rist_parse_udp_address2(url, &cfg);
19 if (ret != 0 || cfg == NULL) {
20 fprintf(stderr, "FAIL: rist_parse_udp_address2(%s) ret=%d cfg=%p\n",
21 url, ret, (void *)cfg);
22 if (cfg)
23 rist_udp_config_free2(&cfg);
24 return 1;
25 }
26 int bad = 0;
27 if (strcmp(cfg->prefix, want_prefix) != 0) {
28 fprintf(stderr, "FAIL: %s -> prefix=\"%s\" (want \"%s\")\n",
29 url, cfg->prefix, want_prefix);
30 bad = 1;
31 }
32 if (cfg->rtp != want_rtp) {
33 fprintf(stderr, "FAIL: %s -> rtp=%d (want %d)\n", url, cfg->rtp, want_rtp);
34 bad = 1;
35 }
36 rist_udp_config_free2(&cfg);
37 return bad;
38 }
39
40 /* Must not crash or corrupt memory; we don't assert on the exact prefix here,
41 * only that the call returns without an out-of-bounds write (ASan gate). */
42 static int expect_no_crash(const char *url)
43 {
44 struct rist_udp_config *cfg = NULL;
45 int ret = rist_parse_udp_address2(url, &cfg);
46 (void)ret;
47 if (cfg)
48 rist_udp_config_free2(&cfg);
49 return 0;
50 }
51
52 int main(void)
53 {
54 int failures = 0;
55
56 /* Normal schemes. */
57 failures += expect_prefix("udp://127.0.0.1:1234", "udp", 0);
58 failures += expect_prefix("rtp://127.0.0.1:1234", "rtp", 1);
59
60 /* Leading '/': prefix_len is 0. The old code passed prefix_len-1 (SIZE_MAX)
61 as the strncpy length and NUL-padded gigabytes over the 16-byte buffer. */
62 failures += expect_no_crash("/127.0.0.1:1234");
63
64 /* Scheme longer than the 16-byte prefix buffer: the old code wrote the NUL
65 terminator at prefix[prefix_len], out of bounds. */
66 failures += expect_no_crash("averylongschemename://host:1234");
67 failures += expect_no_crash("0123456789abcdef0123456789/host");
68
69 if (failures) {
70 fprintf(stderr, "%d failure(s)\n", failures);
71 return 1;
72 }
73 printf("all udp prefix parse cases passed\n");
74 return 0;
75 }