@@ -155,42 +155,40 @@ lyb_parse_union(const void *lyb_data, size_t lyb_data_len, uint32_t *type_idx, c
155155 *
156156 * RFC7950 Section 15.5 defines the appropriate error app tag of "require-instance".
157157 *
158- * @param[in,out] err Error record to be updated
159- * @param[in] type leafref type used to extract target path
160- * @param[in] value value attempting to be stored
161- * @param[in] value_len Length of value that was attempted to be stored .
158+ * @param[in,out] err Error record to be updated.
159+ * @param[in] type Leafref type used to extract target path.
160+ * @param[in] value Value attempted to be stored.
161+ * @param[in] value_len Length of @p value .
162162 * @return LY_ERR value. Only possible errors are LY_SUCCESS and LY_EMEM.
163163 */
164164static LY_ERR
165165union_update_lref_err (struct ly_err_item * err , const struct lysc_type * type , const void * value , size_t value_len )
166166{
167167 const struct lysc_type_leafref * lref ;
168168 char * valstr = NULL ;
169- int msg_len ;
169+ int r ;
170170
171- if ((err == NULL ) || (type -> basetype != LY_TYPE_LEAFREF )) {
171+ if (!err || (type -> basetype != LY_TYPE_LEAFREF )) {
172+ /* nothing to do */
172173 return LY_SUCCESS ;
173174 }
174175
175176 lref = (const struct lysc_type_leafref * )type ;
177+
178+ /* update error-app-tag */
176179 free (err -> apptag );
177180 err -> apptag = strdup ("instance-required" );
178- if (err -> apptag == NULL ) {
179- return LY_EMEM ;
180- }
181+ LY_CHECK_ERR_RET (!err -> apptag , LOGMEM (NULL ), LY_EMEM );
181182
182- free (err -> msg );
183- err -> msg = NULL ;
184183 valstr = strndup ((const char * )value , value_len );
185- if (valstr == NULL ) {
186- return LY_EMEM ;
187- }
184+ LY_CHECK_ERR_RET (!valstr , LOGMEM (NULL ), LY_EMEM );
188185
189- msg_len = asprintf (& err -> msg , LY_ERRMSG_NOLREF_VAL , valstr , lyxp_get_expr (lref -> path ));
186+ /* update error-message */
187+ free (err -> msg );
188+ r = asprintf (& err -> msg , LY_ERRMSG_NOLREF_VAL , valstr , lyxp_get_expr (lref -> path ));
190189 free (valstr );
191- if (msg_len == -1 ) {
192- return LY_EMEM ;
193- }
190+ LY_CHECK_ERR_RET (r == -1 , LOGMEM (NULL ), LY_EMEM );
191+
194192 return LY_SUCCESS ;
195193}
196194
@@ -214,7 +212,7 @@ union_store_type(const struct ly_ctx *ctx, struct lysc_type_union *type_u, uint3
214212 uint32_t options , ly_bool validate , const struct lyd_node * ctx_node , const struct lyd_node * tree ,
215213 struct lys_glob_unres * unres , struct ly_err_item * * err )
216214{
217- LY_ERR urc , rc = LY_SUCCESS ;
215+ LY_ERR rc = LY_SUCCESS ;
218216 struct lysc_type * type = type_u -> types [type_idx ];
219217 const void * value = NULL ;
220218 size_t value_len = 0 ;
@@ -231,15 +229,12 @@ union_store_type(const struct ly_ctx *ctx, struct lysc_type_union *type_u, uint3
231229 /* value of another type, first store the value properly and then use its JSON value for parsing */
232230 rc = type_u -> types [ti ]-> plugin -> store (ctx , type_u -> types [ti ], value , value_len , LYPLG_TYPE_STORE_ONLY ,
233231 subvalue -> format , subvalue -> prefix_data , subvalue -> hints , subvalue -> ctx_node , & subvalue -> value , unres , err );
234- if (( rc != LY_SUCCESS ) && (rc != LY_EINCOMPLETE )) {
232+ if (rc && (rc != LY_EINCOMPLETE )) {
235233 /* clear any leftover/freed garbage */
236234 memset (& subvalue -> value , 0 , sizeof subvalue -> value );
237235
238236 /* if this is a leafref, lets make sure we propagate the appropriate error, and not a type validation failure */
239- urc = union_update_lref_err (* err , type_u -> types [ti ], value , value_len );
240- if (urc != LY_SUCCESS ) {
241- rc = urc ;
242- }
237+ union_update_lref_err (* err , type_u -> types [ti ], value , value_len );
243238 goto cleanup ;
244239 }
245240
@@ -272,15 +267,12 @@ union_store_type(const struct ly_ctx *ctx, struct lysc_type_union *type_u, uint3
272267
273268 rc = type -> plugin -> store (ctx , type , value , value_len , opts , format , prefix_data , subvalue -> hints ,
274269 subvalue -> ctx_node , & subvalue -> value , unres , err );
275- if (( rc != LY_SUCCESS ) && (rc != LY_EINCOMPLETE )) {
270+ if (rc && (rc != LY_EINCOMPLETE )) {
276271 /* clear any leftover/freed garbage */
277272 memset (& subvalue -> value , 0 , sizeof subvalue -> value );
278273
279274 /* if this is a leafref, lets make sure we propagate the appropriate error, and not a type validation failure */
280- urc = union_update_lref_err (* err , type , value , value_len );
281- if (urc != LY_SUCCESS ) {
282- rc = urc ;
283- }
275+ union_update_lref_err (* err , type , value , value_len );
284276 goto cleanup ;
285277 }
286278
@@ -290,6 +282,7 @@ union_store_type(const struct ly_ctx *ctx, struct lysc_type_union *type_u, uint3
290282 if (rc ) {
291283 /* validate failed, we need to free the stored value */
292284 type -> plugin -> free (ctx , & subvalue -> value );
285+ goto cleanup ;
293286 }
294287 }
295288
0 commit comments