Skip to content

Commit 90ed0ea

Browse files
committed
plugins types UPDATE generate no memory errors
1 parent d240097 commit 90ed0ea

20 files changed

Lines changed: 86 additions & 65 deletions

‎src/log.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *data_
179179
return e->err;
180180
}
181181

182+
LIBYANG_API_DEF LY_ERR
183+
ly_err_new_no_mem(struct ly_err_item **err)
184+
{
185+
return ly_err_new(err, LY_EMEM, LYVE_DATA, NULL, NULL, "Memory allocation failed.");
186+
}
187+
182188
LIBYANG_API_DEF const struct ly_err_item *
183189
ly_err_first(const struct ly_ctx *ctx)
184190
{

‎src/plugins_types.h‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,12 @@ enum lyplg_lyb_size_type {
197197
do { if (LYPLG_TYPE_VAL_IS_DYN(type_val)) free(type_val); } while(0)
198198

199199
/**
200-
* @brief Create and fill error structure.
200+
* @brief Create and fill an error structure.
201201
*
202202
* Helper function for various plugin functions to generate error information structure.
203203
*
204+
* The structure can be freed using ::ly_err_free() or passed back from callback into libyang.
205+
*
204206
* @param[in,out] err Pointer to store a new error structure filled according to the input parameters. If the storage
205207
* already contains error information, the new record is appended into the errors list.
206208
* @param[in] ecode Code of the error to fill. In case LY_SUCCESS value, nothing is done and LY_SUCCESS is returned.
@@ -210,14 +212,22 @@ enum lyplg_lyb_size_type {
210212
* @param[in] err_format Format string (same like at printf) or string literal.
211213
* If you want to print just an unknown string, use "%s" for the @p err_format, otherwise undefined behavior may occur
212214
* because the unknown string may contain the % character, which is interpreted as conversion specifier.
213-
* @return The given @p ecode value if the @p err is successfully created. The structure can be freed using ::ly_err_free()
214-
* or passed back from callback into libyang.
215-
* @return LY_EMEM If there is not enough memory for allocating error record, the @p err is not touched in that case.
215+
* @return The given @p ecode value if the @p err is successfully created.
216+
* @return LY_EMEM if there is not enough memory for allocating error record, the @p err is not touched in that case.
216217
* @return LY_SUCCESS if @p ecode is LY_SUCCESS, the @p err is not touched in this case.
217218
*/
218219
LIBYANG_API_DECL LY_ERR ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *data_path, char *apptag,
219220
const char *err_format, ...) _FORMAT_PRINTF(6, 7);
220221

222+
/**
223+
* @brief Create and fill an error structure with a no-memory error.
224+
*
225+
* @param[in,out] err Pointer to store a new error structure filled according to the input parameters. If the storage
226+
* already contains error information, the new record is appended into the errors list.
227+
* @return LY_EMEM.
228+
*/
229+
LIBYANG_API_DECL LY_ERR ly_err_new_no_mem(struct ly_err_item **err);
230+
221231
/**
222232
* @brief Destructor for the error records created with ::ly_err_new().
223233
*

‎src/plugins_types/binary.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ lyplg_type_store_binary(const struct ly_ctx *ctx, const struct lysc_type *type,
267267
/* init storage */
268268
memset(storage, 0, sizeof *storage);
269269
LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
270-
LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
270+
LY_CHECK_ERR_GOTO(!val, ret = ly_err_new_no_mem(err), cleanup);
271271
storage->realtype = type;
272272

273273
/* check value length */
@@ -282,11 +282,11 @@ lyplg_type_store_binary(const struct ly_ctx *ctx, const struct lysc_type *type,
282282
options &= ~LYPLG_TYPE_STORE_DYNAMIC;
283283
} else if (value_size) {
284284
val->data = malloc(value_size);
285-
LY_CHECK_ERR_GOTO(!val->data, ret = LY_EMEM, cleanup);
285+
LY_CHECK_ERR_GOTO(!val->data, ret = ly_err_new_no_mem(err), cleanup);
286286
memcpy(val->data, value, value_size);
287287
} else {
288288
val->data = strdup("");
289-
LY_CHECK_ERR_GOTO(!val->data, ret = LY_EMEM, cleanup);
289+
LY_CHECK_ERR_GOTO(!val->data, ret = ly_err_new_no_mem(err), cleanup);
290290
}
291291

292292
/* store size */

‎src/plugins_types/bits.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ lyplg_type_store_bits(const struct ly_ctx *ctx, const struct lysc_type *type, co
305305
/* init storage */
306306
memset(storage, 0, sizeof *storage);
307307
LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
308-
LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
308+
LY_CHECK_ERR_GOTO(!val, ret = ly_err_new_no_mem(err), cleanup);
309309
storage->realtype = type;
310310

311311
/* check value length */
@@ -320,12 +320,12 @@ lyplg_type_store_bits(const struct ly_ctx *ctx, const struct lysc_type *type, co
320320
options &= ~LYPLG_TYPE_STORE_DYNAMIC;
321321
} else {
322322
val->bitmap = malloc(value_size);
323-
LY_CHECK_ERR_GOTO(!val->bitmap, ret = LY_EMEM, cleanup);
323+
LY_CHECK_ERR_GOTO(!val->bitmap, ret = ly_err_new_no_mem(err), cleanup);
324324
memcpy(val->bitmap, value, value_size);
325325
}
326326

327327
/* allocate and fill the bit item array */
328-
LYA_PREALLOC(val->items, LYA_COUNT(type_bits->bits), LOGMEM(ctx); ret = LY_EMEM; goto cleanup);
328+
LYA_PREALLOC(val->items, LYA_COUNT(type_bits->bits), ret = ly_err_new_no_mem(err); goto cleanup);
329329
LY_CHECK_GOTO(ret = bits_bitmap2items(val->bitmap, type_bits, val->items, err), cleanup);
330330

331331
/* success */
@@ -338,14 +338,14 @@ lyplg_type_store_bits(const struct ly_ctx *ctx, const struct lysc_type *type, co
338338

339339
/* allocate the bitmap */
340340
val->bitmap = calloc(1, LYPLG_BITS2BYTES(BITS_LAST_BIT_POSITION(type_bits) + 1));
341-
LY_CHECK_ERR_GOTO(!val->bitmap, ret = LY_EMEM, cleanup);
341+
LY_CHECK_ERR_GOTO(!val->bitmap, ret = ly_err_new_no_mem(err), cleanup);
342342

343343
/* fill the bitmap */
344344
ret = bits_str2bitmap(value, value_size, type_bits, val->bitmap, err);
345345
LY_CHECK_GOTO(ret, cleanup);
346346

347347
/* allocate and fill the bit item array */
348-
LYA_PREALLOC(val->items, LYA_COUNT(type_bits->bits), LOGMEM(ctx); ret = LY_EMEM; goto cleanup);
348+
LYA_PREALLOC(val->items, LYA_COUNT(type_bits->bits), ret = ly_err_new_no_mem(err); goto cleanup);
349349
LY_CHECK_GOTO(ret = bits_bitmap2items(val->bitmap, type_bits, val->items, err), cleanup);
350350

351351
if (format == LY_VALUE_CANON) {
@@ -356,7 +356,7 @@ lyplg_type_store_bits(const struct ly_ctx *ctx, const struct lysc_type *type, co
356356
LY_CHECK_GOTO(ret, cleanup);
357357
} else {
358358
ret = lydict_insert(ctx, value_size ? value : "", value_size, &storage->_canonical);
359-
LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
359+
LY_CHECK_GOTO(ret, cleanup);
360360
}
361361
}
362362

‎src/plugins_types/date.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ lyplg_type_store_date(const struct ly_ctx *ctx, const struct lysc_type *type, co
7474
} else {
7575
LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val_nz);
7676
}
77-
LY_CHECK_ERR_GOTO(!val && !val_nz, ret = LY_EMEM, cleanup);
77+
LY_CHECK_ERR_GOTO(!val && !val_nz, ret = ly_err_new_no_mem(err), cleanup);
7878
storage->realtype = type;
7979

8080
if (format == LY_VALUE_LYB) {
@@ -133,7 +133,7 @@ lyplg_type_store_date(const struct ly_ctx *ctx, const struct lysc_type *type, co
133133
if (val) {
134134
/* create date-and-time value */
135135
if (asprintf(&str, "%.*sT00:00:00%.*s", 10, (char *)value, (int)(value_size - 10), (char *)value + 10) == -1) {
136-
ret = LY_EMEM;
136+
ret = ly_err_new_no_mem(err);
137137
goto cleanup;
138138
}
139139

‎src/plugins_types/date_and_time.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ lyplg_type_store_date_and_time(const struct ly_ctx *ctx, const struct lysc_type
5959
/* init storage */
6060
memset(storage, 0, sizeof *storage);
6161
LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
62-
LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
62+
LY_CHECK_ERR_GOTO(!val, ret = ly_err_new_no_mem(err), cleanup);
6363
storage->realtype = type;
6464

6565
if (format == LY_VALUE_LYB) {
@@ -85,7 +85,7 @@ lyplg_type_store_date_and_time(const struct ly_ctx *ctx, const struct lysc_type
8585
/* store fractions of second */
8686
if (value_size > 9) {
8787
val->fractions_s = strndup(((char *)value) + 9, value_size - 9);
88-
LY_CHECK_ERR_GOTO(!val->fractions_s, ret = LY_EMEM, cleanup);
88+
LY_CHECK_ERR_GOTO(!val->fractions_s, ret = ly_err_new_no_mem(err), cleanup);
8989
}
9090

9191
/* store unknown timezone */

‎src/plugins_types/hex_string.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ lyplg_type_store_hex_string(const struct ly_ctx *ctx, const struct lysc_type *ty
6060
/* make a copy, it is needed for canonization */
6161
if ((format != LY_VALUE_CANON) && !(options & LYPLG_TYPE_STORE_DYNAMIC)) {
6262
value = strndup(value, value_size);
63-
LY_CHECK_ERR_GOTO(!value, ret = LY_EMEM, cleanup);
63+
LY_CHECK_ERR_GOTO(!value, ret = ly_err_new_no_mem(err), cleanup);
6464
options |= LYPLG_TYPE_STORE_DYNAMIC;
6565
}
6666

‎src/plugins_types/identityref.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,7 @@ lyplg_type_store_identityref(const struct ly_ctx *ctx, const struct lysc_type *t
285285
} else {
286286
/* JSON format with prefix is the canonical one */
287287
if (asprintf(&canon, "%s:%s", ident->module->name, ident->name) == -1) {
288-
LOGMEM(ctx);
289-
ret = LY_EMEM;
288+
ret = ly_err_new_no_mem(err);
290289
goto cleanup;
291290
}
292291

‎src/plugins_types/instanceid_keys.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ instanceid_keys_print_value(const struct lyd_value_instance_identifier_keys *val
9292
mem = realloc(*str_value, str_len + strlen(str_tok) + 1);
9393
if (!mem) {
9494
free(str_tok);
95-
ret = ly_err_new(err, LY_EMEM, LYVE_DATA, NULL, NULL, "No memory.");
95+
ret = ly_err_new_no_mem(err);
9696
goto cleanup;
9797
}
9898
*str_value = mem;
@@ -105,7 +105,7 @@ instanceid_keys_print_value(const struct lyd_value_instance_identifier_keys *val
105105
/* just copy the token */
106106
mem = realloc(*str_value, str_len + val->keys->tok_len[cur_idx] + 1);
107107
if (!mem) {
108-
ret = ly_err_new(err, LY_EMEM, LYVE_DATA, NULL, NULL, "No memory.");
108+
ret = ly_err_new_no_mem(err);
109109
goto cleanup;
110110
}
111111
*str_value = mem;
@@ -145,7 +145,7 @@ lyplg_type_store_instanceid_keys(const struct ly_ctx *ctx, const struct lysc_typ
145145
/* init storage */
146146
memset(storage, 0, sizeof *storage);
147147
LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
148-
LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
148+
LY_CHECK_ERR_GOTO(!val, ret = ly_err_new_no_mem(err), cleanup);
149149
storage->realtype = type;
150150

151151
/* check value length */

‎src/plugins_types/integer.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,16 @@ lyplg_type_store_int(const struct ly_ctx *ctx, const struct lysc_type *type, con
135135
/* generate canonical value */
136136
switch (type->basetype) {
137137
case LY_TYPE_INT8:
138-
LY_CHECK_ERR_GOTO(asprintf(&canon, "%" PRId8, storage->int8) == -1, ret = LY_EMEM, cleanup);
138+
LY_CHECK_ERR_GOTO(asprintf(&canon, "%" PRId8, storage->int8) == -1, ret = ly_err_new_no_mem(err), cleanup);
139139
break;
140140
case LY_TYPE_INT16:
141-
LY_CHECK_ERR_GOTO(asprintf(&canon, "%" PRId16, storage->int16) == -1, ret = LY_EMEM, cleanup);
141+
LY_CHECK_ERR_GOTO(asprintf(&canon, "%" PRId16, storage->int16) == -1, ret = ly_err_new_no_mem(err), cleanup);
142142
break;
143143
case LY_TYPE_INT32:
144-
LY_CHECK_ERR_GOTO(asprintf(&canon, "%" PRId32, storage->int32) == -1, ret = LY_EMEM, cleanup);
144+
LY_CHECK_ERR_GOTO(asprintf(&canon, "%" PRId32, storage->int32) == -1, ret = ly_err_new_no_mem(err), cleanup);
145145
break;
146146
case LY_TYPE_INT64:
147-
LY_CHECK_ERR_GOTO(asprintf(&canon, "%" PRId64, storage->int64) == -1, ret = LY_EMEM, cleanup);
147+
LY_CHECK_ERR_GOTO(asprintf(&canon, "%" PRId64, storage->int64) == -1, ret = ly_err_new_no_mem(err), cleanup);
148148
break;
149149
default:
150150
break;
@@ -380,7 +380,7 @@ lyplg_type_store_uint(const struct ly_ctx *ctx, const struct lysc_type *type, co
380380
}
381381
} else {
382382
/* generate canonical value */
383-
LY_CHECK_ERR_GOTO(asprintf(&canon, "%" PRIu64, num) == -1, ret = LY_EMEM, cleanup);
383+
LY_CHECK_ERR_GOTO(asprintf(&canon, "%" PRIu64, num) == -1, ret = ly_err_new_no_mem(err), cleanup);
384384

385385
/* store it */
386386
ret = lydict_insert_zc(ctx, canon, (const char **)&storage->_canonical);

0 commit comments

Comments
 (0)