From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Date: Sat, 29 Dec 2018 19:25:46 +0300 From: Vladimir Davydov Subject: Re: [tarantool-patches] Re: [PATCH v1 4/4] box: refactor tuple_init_field_map to use bitmap Message-ID: <20181229162546.npbzbaasxe6ufk3p@esperanza> References: <20181227191249.vojcr2jl3ugnm5pw@esperanza> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: To: Kirill Shcherbatov Cc: tarantool-patches@freelists.org List-ID: On Sat, Dec 29, 2018 at 03:58:34PM +0300, Kirill Shcherbatov wrote: > Refactored tuple_init_field_map to fill a local bitmap and It's not refactoring, it's a rework. > compare it with template required_fields bitmap containing > information about required fields. Each field is mapped to > bitmap with field:id - unique field identifier. > This approach to check the required fields will work even after > the introduction of JSON paths, when the field tree becomes > multilevel. > > Needed for #1012 > @@ -440,15 +484,30 @@ tuple_init_field_map(struct tuple_format *format, uint32_t *field_map, > (unsigned) format->exact_field_count); > return -1; > } > - if (validate && field_count < format->min_field_count) { > - diag_set(ClientError, ER_MIN_FIELD_COUNT, > - (unsigned) field_count, > - (unsigned) format->min_field_count); > - return -1; > - } > > /* first field is simply accessible, so we do not store offset to it */ > struct tuple_field *field = tuple_format_field(format, 0); > + /* > + * Allocate fields_bitmap - a copy of the initialized > + * format:required_fields bitmap. The field:id bits would > + * be nullified for founded fields during tuple parse to > + * raise an error when some required field is missing. > + */ > + struct region *region = &fiber()->gc; > + char *fields_bitmap = NULL; > + uint32_t fields_bitmap_sz = bitmap_size(format->total_field_count); > + if (validate) { > + fields_bitmap = region_alloc(region, fields_bitmap_sz); > + if (fields_bitmap == NULL) { > + diag_set(OutOfMemory, fields_bitmap_sz, "calloc", s/calloc/region Memory allocated on the region isn't freed when the function returns... > + "required_fields"); > + return -1; > + } > + memcpy(fields_bitmap, format->required_fields, > + fields_bitmap_sz); > + if (field_count > 0) > + bit_clear(fields_bitmap, field->id); > + } > if (validate && > !field_mp_type_is_compatible(field->type, mp_typeof(*pos), > tuple_field_is_nullable(field))) { If field_count is 0, then you'll access uninitialized memory here ^^^ I'm kinda tired of reviewing this simple patch so I fixed these issues by myself, fixed grammar in the comments, and pushed the patch to 2.1.