Debian Codegraph @

Xpjproject/tests/fuzz/fuzz-presence.c

raw symbols SCIP
1 #include <stdint.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <limits.h>
5 #include <pjsip.h>
6 #include <pjlib.h>
7 #include <pjsip-simple/pidf.h>
8 #include <pjsip-simple/xpidf.h>
9 #include <pjsip-simple/rpid.h>
10 #include <pjsip-simple/dialog_info.h>
11 #include <pjsip-simple/iscomposing.h>
12
13 #define kMinInputLength 10
14 #define kMaxInputLength 5120
15
16 static pj_caching_pool caching_pool;
17 static pj_pool_t *pool = NULL;
18
19 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
20 char print_buf[2048];
21 pj_status_t status;
22 int doc_len;
23
24 if (Size < kMinInputLength || Size > kMaxInputLength) {
25 return 0;
26 }
27
28 status = pj_init();
29 if (status != PJ_SUCCESS) {
30 return 0;
31 }
32
33 pj_log_set_level(0);
34 pj_caching_pool_init(&caching_pool, &pj_pool_factory_default_policy, 0);
35
36 pool = pj_pool_create(&caching_pool.factory, "fuzz-presence", 4000, 4000, NULL);
37 if (!pool) {
38 pj_caching_pool_destroy(&caching_pool);
39 return 0;
40 }
41
42 /* Clamp size to INT_MAX to avoid truncation issues */
43 if (Size > INT_MAX) {
44 Size = INT_MAX;
45 }
46 doc_len = (int)Size;
47
48 char *doc = pj_pool_alloc(pool, Size + 1);
49 if (!doc) {
50 pj_pool_release(pool);
51 pj_caching_pool_destroy(&caching_pool);
52 return 0;
53 }
54 memcpy(doc, Data, Size);
55 doc[Size] = '\0';
56
57 /* Test PIDF parser */
58 pjpidf_pres *pidf_pres = pjpidf_parse(pool, doc, doc_len);
59 if (pidf_pres) {
60 pjpidf_print(pidf_pres, print_buf, sizeof(print_buf));
61
62 pjpidf_tuple *tuple = pjpidf_pres_get_first_tuple(pidf_pres);
63 if (tuple) {
64 /* Call only safe accessor functions without assertions */
65 pjpidf_tuple_get_contact(tuple);
66 pjpidf_tuple_get_timestamp(tuple);
67
68 pjpidf_note *note = pjpidf_tuple_get_first_note(tuple);
69 while (note) {
70 note = pjpidf_tuple_get_next_note(tuple, note);
71 }
72 }
73
74 pjpidf_pres_get_first_note(pidf_pres);
75
76 /* Test RPID within PIDF */
77 pjrpid_element rpid_elem;
78 if (pjrpid_get_element(pidf_pres, pool, &rpid_elem) == PJ_SUCCESS) {
79 (void)rpid_elem.type;
80 (void)rpid_elem.activity;
81 if (rpid_elem.note.slen > 0) {
82 (void)rpid_elem.note.ptr;
83 }
84 }
85 }
86
87 /* Test XPIDF parser */
88 pjxpidf_pres *xpidf_pres = pjxpidf_parse(pool, doc, (pj_size_t)Size);
89 if (xpidf_pres) {
90 pjxpidf_print(xpidf_pres, print_buf, sizeof(print_buf));
91 pjxpidf_get_uri(xpidf_pres);
92 pjxpidf_get_status(xpidf_pres);
93 }
94
95 /* Test Dialog-Info parser */
96 pjsip_dlg_info_dialog *dlg_info = pjsip_dlg_info_parse(pool, doc, doc_len);
97 if (dlg_info) {
98 pj_xml_attr *attr = dlg_info->attr_head.next;
99 while (attr != &dlg_info->attr_head) {
100 if (attr->name.slen > 0) (void)attr->name.ptr;
101 if (attr->value.slen > 0) (void)attr->value.ptr;
102 attr = attr->next;
103 }
104
105 pj_xml_node *child = dlg_info->node_head.next;
106 while (child != (pj_xml_node*)&dlg_info->node_head) {
107 if (child->name.slen > 0) (void)child->name.ptr;
108 if (child->content.slen > 0) (void)child->content.ptr;
109 child = child->next;
110 }
111 }
112
113 /* Test Iscomposing parser */
114 pj_bool_t is_composing = PJ_FALSE;
115 pj_str_t *last_active = NULL;
116 pj_str_t *content_type = NULL;
117 int refresh = 0;
118
119 if (pjsip_iscomposing_parse(pool, doc, (pj_size_t)Size,
120 &is_composing, &last_active,
121 &content_type, &refresh) == PJ_SUCCESS) {
122 (void)is_composing;
123 (void)refresh;
124 if (last_active && last_active->slen > 0) (void)last_active->ptr;
125 if (content_type && content_type->slen > 0) (void)content_type->ptr;
126 }
127
128 pj_pool_release(pool);
129 pj_caching_pool_destroy(&caching_pool);
130 return 0;
131 }
132