1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 #include "upb/util/required_fields.h"
9
10 #include <assert.h>
11 #include <inttypes.h>
12 #include <setjmp.h>
13 #include <stdarg.h>
14 #include <stdbool.h>
15 #include <stddef.h>
16 #include <stdlib.h>
17 #include <string.h>
18
19 #include "upb/base/descriptor_constants.h"
20 #include "upb/mem/alloc.h"
21 #include "upb/message/array.h"
22 #include "upb/message/map.h"
23 #include "upb/message/message.h"
24 #include "upb/port/vsnprintf_compat.h"
25 #include "upb/reflection/def.h"
26 #include "upb/reflection/message.h"
27
28 // Must be last.
29 #include "upb/port/def.inc"
30
31 ////////////////////////////////////////////////////////////////////////////////
32 // upb_FieldPath_ToText()
33 ////////////////////////////////////////////////////////////////////////////////
34
35 typedef struct {
36 char* buf;
37 char* ptr;
38 char* end;
39 size_t overflow;
40 } upb_PrintfAppender;
41
42 UPB_PRINTF(2, 3)
upb_FieldPath_Printf(upb_PrintfAppender * a,const char * fmt,...)43 static void upb_FieldPath_Printf(upb_PrintfAppender* a, const char* fmt, ...) {
44 size_t n;
45 size_t have = a->end - a->ptr;
46 va_list args;
47
48 va_start(args, fmt);
49 n = _upb_vsnprintf(a->ptr, have, fmt, args);
50 va_end(args);
51
52 if (UPB_LIKELY(have > n)) {
53 // We can't end up here if the user passed (NULL, 0), therefore ptr is known
54 // to be non-NULL, and UPB_PTRADD() is not necessary.
55 assert(a->ptr);
56 a->ptr += n;
57 } else {
58 a->ptr = UPB_PTRADD(a->ptr, have);
59 a->overflow += (n - have);
60 }
61 }
62
upb_FieldPath_NullTerminate(upb_PrintfAppender * d,size_t size)63 static size_t upb_FieldPath_NullTerminate(upb_PrintfAppender* d, size_t size) {
64 size_t ret = d->ptr - d->buf + d->overflow;
65
66 if (size > 0) {
67 if (d->ptr == d->end) d->ptr--;
68 *d->ptr = '\0';
69 }
70
71 return ret;
72 }
73
upb_FieldPath_PutMapKey(upb_PrintfAppender * a,upb_MessageValue map_key,const upb_FieldDef * key_f)74 static void upb_FieldPath_PutMapKey(upb_PrintfAppender* a,
75 upb_MessageValue map_key,
76 const upb_FieldDef* key_f) {
77 switch (upb_FieldDef_CType(key_f)) {
78 case kUpb_CType_Int32:
79 upb_FieldPath_Printf(a, "[%" PRId32 "]", map_key.int32_val);
80 break;
81 case kUpb_CType_Int64:
82 upb_FieldPath_Printf(a, "[%" PRId64 "]", map_key.int64_val);
83 break;
84 case kUpb_CType_UInt32:
85 upb_FieldPath_Printf(a, "[%" PRIu32 "]", map_key.uint32_val);
86 break;
87 case kUpb_CType_UInt64:
88 upb_FieldPath_Printf(a, "[%" PRIu64 "]", map_key.uint64_val);
89 break;
90 case kUpb_CType_Bool:
91 upb_FieldPath_Printf(a, "[%s]", map_key.bool_val ? "true" : "false");
92 break;
93 case kUpb_CType_String:
94 upb_FieldPath_Printf(a, "[\"");
95 for (size_t i = 0; i < map_key.str_val.size; i++) {
96 char ch = map_key.str_val.data[i];
97 if (ch == '"') {
98 upb_FieldPath_Printf(a, "\\\"");
99 } else {
100 upb_FieldPath_Printf(a, "%c", ch);
101 }
102 }
103 upb_FieldPath_Printf(a, "\"]");
104 break;
105 default:
106 UPB_UNREACHABLE(); // Other types can't be map keys.
107 }
108 }
109
upb_FieldPath_ToText(upb_FieldPathEntry ** path,char * buf,size_t size)110 size_t upb_FieldPath_ToText(upb_FieldPathEntry** path, char* buf, size_t size) {
111 upb_FieldPathEntry* ptr = *path;
112 upb_PrintfAppender appender;
113 appender.buf = buf;
114 appender.ptr = buf;
115 appender.end = UPB_PTRADD(buf, size);
116 appender.overflow = 0;
117 bool first = true;
118
119 while (ptr->field) {
120 const upb_FieldDef* f = ptr->field;
121
122 upb_FieldPath_Printf(&appender, first ? "%s" : ".%s", upb_FieldDef_Name(f));
123 first = false;
124 ptr++;
125
126 if (upb_FieldDef_IsMap(f)) {
127 const upb_FieldDef* key_f =
128 upb_MessageDef_Field(upb_FieldDef_MessageSubDef(f), 0);
129 upb_FieldPath_PutMapKey(&appender, ptr->map_key, key_f);
130 ptr++;
131 } else if (upb_FieldDef_IsRepeated(f)) {
132 upb_FieldPath_Printf(&appender, "[%zu]", ptr->array_index);
133 ptr++;
134 }
135 }
136
137 // Advance beyond terminating NULL.
138 ptr++;
139 *path = ptr;
140 return upb_FieldPath_NullTerminate(&appender, size);
141 }
142
143 ////////////////////////////////////////////////////////////////////////////////
144 // upb_util_HasUnsetRequired()
145 ////////////////////////////////////////////////////////////////////////////////
146
147 typedef struct {
148 upb_FieldPathEntry* path;
149 size_t size;
150 size_t cap;
151 } upb_FieldPathVector;
152
153 typedef struct {
154 upb_FieldPathVector stack;
155 upb_FieldPathVector out_fields;
156 const upb_DefPool* ext_pool;
157 jmp_buf err;
158 bool has_unset_required;
159 bool save_paths;
160 } upb_FindContext;
161
upb_FieldPathVector_Init(upb_FieldPathVector * vec)162 static void upb_FieldPathVector_Init(upb_FieldPathVector* vec) {
163 vec->path = NULL;
164 vec->size = 0;
165 vec->cap = 0;
166 }
167
upb_FieldPathVector_Reserve(upb_FindContext * ctx,upb_FieldPathVector * vec,size_t elems)168 static void upb_FieldPathVector_Reserve(upb_FindContext* ctx,
169 upb_FieldPathVector* vec,
170 size_t elems) {
171 if (vec->cap - vec->size < elems) {
172 const int oldsize = vec->cap * sizeof(*vec->path);
173 size_t need = vec->size + elems;
174 vec->cap = UPB_MAX(4, vec->cap);
175 while (vec->cap < need) vec->cap *= 2;
176 const int newsize = vec->cap * sizeof(*vec->path);
177 vec->path = upb_grealloc(vec->path, oldsize, newsize);
178 if (!vec->path) {
179 UPB_LONGJMP(ctx->err, 1);
180 }
181 }
182 }
183
upb_FindContext_Push(upb_FindContext * ctx,upb_FieldPathEntry ent)184 static void upb_FindContext_Push(upb_FindContext* ctx, upb_FieldPathEntry ent) {
185 if (!ctx->save_paths) return;
186 upb_FieldPathVector_Reserve(ctx, &ctx->stack, 1);
187 ctx->stack.path[ctx->stack.size++] = ent;
188 }
189
upb_FindContext_Pop(upb_FindContext * ctx)190 static void upb_FindContext_Pop(upb_FindContext* ctx) {
191 if (!ctx->save_paths) return;
192 assert(ctx->stack.size != 0);
193 ctx->stack.size--;
194 }
195
upb_util_FindUnsetInMessage(upb_FindContext * ctx,const upb_Message * msg,const upb_MessageDef * m)196 static void upb_util_FindUnsetInMessage(upb_FindContext* ctx,
197 const upb_Message* msg,
198 const upb_MessageDef* m) {
199 // Iterate over all fields to see if any required fields are missing.
200 for (int i = 0, n = upb_MessageDef_FieldCount(m); i < n; i++) {
201 const upb_FieldDef* f = upb_MessageDef_Field(m, i);
202 if (!upb_FieldDef_IsRequired(f)) continue;
203
204 if (!msg || !upb_Message_HasFieldByDef(msg, f)) {
205 // A required field is missing.
206 ctx->has_unset_required = true;
207
208 if (ctx->save_paths) {
209 // Append the contents of the stack to the out array, then
210 // NULL-terminate.
211 upb_FieldPathVector_Reserve(ctx, &ctx->out_fields, ctx->stack.size + 2);
212 if (ctx->stack.size) {
213 memcpy(&ctx->out_fields.path[ctx->out_fields.size], ctx->stack.path,
214 ctx->stack.size * sizeof(*ctx->stack.path));
215 }
216 ctx->out_fields.size += ctx->stack.size;
217 ctx->out_fields.path[ctx->out_fields.size++] =
218 (upb_FieldPathEntry){.field = f};
219 ctx->out_fields.path[ctx->out_fields.size++] =
220 (upb_FieldPathEntry){.field = NULL};
221 }
222 }
223 }
224 }
225
upb_util_FindUnsetRequiredInternal(upb_FindContext * ctx,const upb_Message * msg,const upb_MessageDef * m)226 static void upb_util_FindUnsetRequiredInternal(upb_FindContext* ctx,
227 const upb_Message* msg,
228 const upb_MessageDef* m) {
229 // OPT: add markers in the schema for where we can avoid iterating:
230 // 1. messages with no required fields.
231 // 2. messages that cannot possibly reach any required fields.
232
233 upb_util_FindUnsetInMessage(ctx, msg, m);
234 if (!msg) return;
235
236 // Iterate over all present fields to find sub-messages that might be missing
237 // required fields. This may revisit some of the fields already inspected
238 // in the previous loop. We do this separately because this loop will also
239 // find present extensions, which the previous loop will not.
240 //
241 // TODO: consider changing upb_Message_Next() to be capable of
242 // visiting extensions only, for example with a kUpb_Message_BeginEXT
243 // constant.
244 size_t iter = kUpb_Message_Begin;
245 const upb_FieldDef* f;
246 upb_MessageValue val;
247 while (upb_Message_Next(msg, m, ctx->ext_pool, &f, &val, &iter)) {
248 // Skip non-submessage fields.
249 if (!upb_FieldDef_IsSubMessage(f)) continue;
250
251 upb_FindContext_Push(ctx, (upb_FieldPathEntry){.field = f});
252 const upb_MessageDef* sub_m = upb_FieldDef_MessageSubDef(f);
253
254 if (upb_FieldDef_IsMap(f)) {
255 // Map field.
256 const upb_FieldDef* val_f = upb_MessageDef_Field(sub_m, 1);
257 const upb_MessageDef* val_m = upb_FieldDef_MessageSubDef(val_f);
258 if (!val_m) continue;
259 const upb_Map* map = val.map_val;
260 size_t iter = kUpb_Map_Begin;
261 upb_MessageValue key, map_val;
262 while (upb_Map_Next(map, &key, &map_val, &iter)) {
263 upb_FindContext_Push(ctx, (upb_FieldPathEntry){.map_key = key});
264 upb_util_FindUnsetRequiredInternal(ctx, map_val.msg_val, val_m);
265 upb_FindContext_Pop(ctx);
266 }
267 } else if (upb_FieldDef_IsRepeated(f)) {
268 // Repeated field.
269 const upb_Array* arr = val.array_val;
270 for (size_t i = 0, n = upb_Array_Size(arr); i < n; i++) {
271 upb_MessageValue elem = upb_Array_Get(arr, i);
272 upb_FindContext_Push(ctx, (upb_FieldPathEntry){.array_index = i});
273 upb_util_FindUnsetRequiredInternal(ctx, elem.msg_val, sub_m);
274 upb_FindContext_Pop(ctx);
275 }
276 } else {
277 // Scalar sub-message field.
278 upb_util_FindUnsetRequiredInternal(ctx, val.msg_val, sub_m);
279 }
280
281 upb_FindContext_Pop(ctx);
282 }
283 }
284
upb_util_HasUnsetRequired(const upb_Message * msg,const upb_MessageDef * m,const upb_DefPool * ext_pool,upb_FieldPathEntry ** fields)285 bool upb_util_HasUnsetRequired(const upb_Message* msg, const upb_MessageDef* m,
286 const upb_DefPool* ext_pool,
287 upb_FieldPathEntry** fields) {
288 upb_FindContext ctx;
289 ctx.has_unset_required = false;
290 ctx.save_paths = fields != NULL;
291 ctx.ext_pool = ext_pool;
292 upb_FieldPathVector_Init(&ctx.stack);
293 upb_FieldPathVector_Init(&ctx.out_fields);
294 upb_util_FindUnsetRequiredInternal(&ctx, msg, m);
295 upb_gfree(ctx.stack.path);
296
297 if (ctx.has_unset_required && fields) {
298 upb_FieldPathVector_Reserve(&ctx, &ctx.out_fields, 1);
299 ctx.out_fields.path[ctx.out_fields.size] =
300 (upb_FieldPathEntry){.field = NULL};
301 *fields = ctx.out_fields.path;
302 }
303
304 return ctx.has_unset_required;
305 }
306