Skip to content

Commit 6a5ca85

Browse files
committed
ly common UPDATE do not store serialized pcodes
1 parent 37e1ae2 commit 6a5ca85

File tree

7 files changed

+40
-157
lines changed

7 files changed

+40
-157
lines changed

src/ly_common.c

Lines changed: 28 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ ly_ctx_ht_pattern_free_cb(void *val_p)
124124
{
125125
struct ly_pattern_ht_rec *val = val_p;
126126

127-
if (val->serialized_pattern) {
127+
if (val->pcode) {
128128
/* free the pcode */
129-
pcre2_serialize_free(val->serialized_pattern);
129+
pcre2_code_free(val->pcode);
130130
}
131131
}
132132

@@ -488,160 +488,54 @@ ly_ctx_data_del(const struct ly_ctx *ctx)
488488
pthread_rwlock_unlock(&ly_ctx_data_rwlock);
489489
}
490490

491-
/**
492-
* @brief Serialize a PCRE2 compiled code.
493-
*
494-
* @param[in] pcode PCRE2 compiled code to serialize.
495-
* @param[out] serialized_code Pointer to the serialized code, must be freed by the caller.
496-
* @return LY_SUCCESS on success, LY_EINT on serialization error.
497-
*/
498-
static LY_ERR
499-
ly_pcode_serialize(const pcre2_code *pcode, uint8_t **serialized_code)
500-
{
501-
int r;
502-
uint8_t *ser_code = NULL;
503-
PCRE2_SIZE ser_code_size = 0;
504-
505-
*serialized_code = NULL;
506-
507-
r = pcre2_serialize_encode(&pcode, 1, &ser_code, &ser_code_size, NULL);
508-
if (r < 0) {
509-
PCRE2_UCHAR err_msg[LY_PCRE2_MSG_LIMIT] = {0};
510-
511-
pcre2_get_error_message(r, err_msg, LY_PCRE2_MSG_LIMIT);
512-
LOGERR(NULL, LY_EINT, "PCRE2 compiled code serialization failed (%s).", err_msg);
513-
return LY_EINT;
514-
}
515-
516-
*serialized_code = ser_code;
517-
return LY_SUCCESS;
518-
}
519-
520-
/**
521-
* @brief Deserialize a PCRE2 compiled code from its serialized form.
522-
*
523-
* This code does not have to be recompiled, it can be used directly.
524-
*
525-
* @param[in] serialized_code Serialized PCRE2 compiled code.
526-
* @param[out] pcode Pointer to the deserialized PCRE2 code, must be freed by the caller.
527-
* @return LY_SUCCESS on success, LY_EINT on deserialization error.
528-
*/
529-
static LY_ERR
530-
ly_pcode_deserialize(const uint8_t *serialized_code, pcre2_code **pcode)
531-
{
532-
int r;
533-
pcre2_code *pcode_tmp = NULL;
534-
535-
*pcode = NULL;
536-
537-
/* deserialize the pcode */
538-
r = pcre2_serialize_decode(&pcode_tmp, 1, serialized_code, NULL);
539-
if (r < 0) {
540-
PCRE2_UCHAR err_msg[LY_PCRE2_MSG_LIMIT] = {0};
541-
542-
pcre2_get_error_message(r, err_msg, LY_PCRE2_MSG_LIMIT);
543-
LOGERR(NULL, LY_EINT, "PCRE2 compiled code deserialization failed (%s).", err_msg);
544-
return LY_EINT;
545-
}
546-
547-
*pcode = pcode_tmp;
548-
return LY_SUCCESS;
549-
}
550-
551-
/**
552-
* @brief Get a compiled PCRE2 pattern code from the context's pattern hash table.
553-
*
554-
* @param[in] ctx Context to get the pattern code from.
555-
* @param[in] pattern Pattern string to search for.
556-
* @param[in] pattern_ht Pattern hash table to search in.
557-
* @param[out] pcode Optional compiled pattern code, must be freed by the caller.
558-
* If NULL, the function only checks if the pattern exists in the hash table.
559-
* @return LY_SUCCESS on success, LY_ENOTFOUND if the pattern was not found,
560-
* other LY_ERR values on error.
561-
*/
562-
static LY_ERR
563-
_ly_ctx_get_pattern_code(const struct ly_ctx *ctx, const char *pattern,
564-
struct ly_ht *pattern_ht, pcre2_code **pcode)
565-
{
566-
LY_ERR rc = LY_SUCCESS;
567-
uint32_t hash;
568-
struct ly_pattern_ht_rec rec = {0}, *found_rec = NULL;
569-
570-
assert(ctx && pattern && pattern_ht);
571-
572-
/* use the pattern as a key */
573-
rec.pattern = pattern;
574-
rec.serialized_pattern = NULL;
575-
hash = lyht_hash(pattern, strlen(pattern));
576-
577-
/* try to find the record */
578-
LY_CHECK_GOTO(rc = lyht_find(pattern_ht, &rec, hash, (void **)&found_rec), cleanup);
579-
580-
if (pcode) {
581-
/* found it, deserialize the pcode */
582-
LY_CHECK_GOTO(rc = ly_pcode_deserialize(found_rec->serialized_pattern, pcode), cleanup);
583-
}
584-
585-
cleanup:
586-
return rc;
587-
}
588-
589491
LY_ERR
590-
ly_ctx_get_pattern_code(const struct ly_ctx *ctx, const char *pattern, pcre2_code **pcode)
492+
ly_ctx_get_or_compile_pattern_code(const struct ly_ctx *ctx, const char *pattern, const pcre2_code **pcode)
591493
{
592494
LY_ERR rc = LY_SUCCESS;
593495
struct ly_ctx_shared_data *ctx_data;
496+
struct ly_pattern_ht_rec rec = {0}, *found_rec = NULL;
497+
uint32_t hash;
498+
pcre2_code *pcode_tmp = NULL;
594499

595-
ctx_data = ly_ctx_shared_data_get(ctx);
596-
LY_CHECK_RET(!ctx_data, LY_EINT);
500+
assert(ctx && pattern);
597501

598-
rc = _ly_ctx_get_pattern_code(ctx, pattern, ctx_data->pattern_ht, pcode);
599-
if (rc) {
600-
LOGERR(ctx, rc, "Failed to get pattern code for \"%s\".", pattern);
502+
if (pcode) {
503+
*pcode = NULL;
601504
}
602505

603-
return rc;
604-
}
605-
606-
LY_ERR
607-
ly_ctx_compile_and_cache_pattern_code(const struct ly_ctx *ctx, const char *pattern)
608-
{
609-
LY_ERR rc = LY_SUCCESS;
610-
struct ly_ctx_shared_data *ctx_data;
611-
uint8_t *serialized_pcode = NULL;
612-
pcre2_code *pcode_tmp = NULL;
613-
struct ly_pattern_ht_rec rec = {0};
614-
uint32_t hash;
615-
616506
ctx_data = ly_ctx_shared_data_get(ctx);
617507
LY_CHECK_RET(!ctx_data, LY_EINT);
618508

619-
/* check for existing pattern code */
620-
rc = _ly_ctx_get_pattern_code(ctx, pattern, ctx_data->pattern_ht, NULL);
621-
LY_CHECK_GOTO(rc && (rc != LY_ENOTFOUND), cleanup);
622-
if (!rc) {
623-
/* pattern is already compiled and stored, nothing to do */
509+
/* try to find the pattern code in the pattern ht */
510+
hash = lyht_hash(pattern, strlen(pattern));
511+
rec.pattern = pattern;
512+
if (!lyht_find(ctx_data->pattern_ht, &rec, hash, (void **)&found_rec)) {
513+
/* found it, return it */
514+
if (pcode) {
515+
*pcode = found_rec->pcode;
516+
}
624517
goto cleanup;
625518
}
626519

627-
/* record not found, we need to compile the pattern and insert it */
520+
/* didnt find it, need to compile it */
628521
LY_CHECK_GOTO(rc = lys_compile_type_pattern_check(ctx, pattern, &pcode_tmp), cleanup);
629522

630-
/* serialize the pcode */
631-
LY_CHECK_GOTO(rc = ly_pcode_serialize(pcode_tmp, &serialized_pcode), cleanup);
632-
633-
/* insert the record */
523+
/* store the compiled pattern code in the hash table */
634524
hash = lyht_hash(pattern, strlen(pattern));
635525
rec.pattern = pattern;
636-
rec.serialized_pattern = serialized_pcode;
526+
rec.pcode = pcode_tmp;
637527
LY_CHECK_GOTO(rc = lyht_insert_no_check(ctx_data->pattern_ht, &rec, hash, NULL), cleanup);
638528

639-
/* dont free the serialized pcode, it is now owned by the record in the hash table */
640-
serialized_pcode = NULL;
529+
if (pcode) {
530+
*pcode = pcode_tmp;
531+
pcode_tmp = NULL;
532+
}
641533

642534
cleanup:
643-
pcre2_code_free(pcode_tmp);
644-
pcre2_serialize_free(serialized_pcode);
535+
if (rc) {
536+
/* only free the pcode if we failed, because it belongs to the hash table */
537+
pcre2_code_free(pcode_tmp);
538+
}
645539
return rc;
646540
}
647541

src/ly_common.h

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -476,25 +476,16 @@ void ly_ctx_ht_leafref_links_rec_free(void *val_p);
476476
struct lys_module *ly_ctx_get_module_implemented2(const struct ly_ctx *ctx, const char *name, size_t name_len);
477477

478478
/**
479-
* @brief Get a compiled PCRE2 pattern code from the context's pattern hash table.
479+
* @brief Get or compile a PCRE2 pattern code.
480480
*
481-
* @param[in] ctx Context to get the pattern code from.
482-
* @param[in] pattern Pattern string to search for.
483-
* @param[out] pcode Compiled pattern code.
484-
* @return LY_ERR value.
485-
*/
486-
LY_ERR ly_ctx_get_pattern_code(const struct ly_ctx *ctx, const char *pattern, pcre2_code **pcode);
487-
488-
/**
489-
* @brief Compile a pattern string into a PCRE2 code and store it in the context's pattern hash table.
490-
*
491-
* If the pattern is already compiled and stored, it is not recompiled.
481+
* If the pattern is not found in the context, it is compiled and cached.
492482
*
493483
* @param[in] ctx Context to get or create the pattern code in.
494484
* @param[in] pattern Pattern string to search for or to compile and store.
485+
* @param[out] pcode Optional compiled pattern code. DO NOT FREE IT, it is owned by the context.
495486
* @return LY_ERR value.
496487
*/
497-
LY_ERR ly_ctx_compile_and_cache_pattern_code(const struct ly_ctx *ctx, const char *pattern);
488+
LY_ERR ly_ctx_get_or_compile_pattern_code(const struct ly_ctx *ctx, const char *pattern, const pcre2_code **pcode);
498489

499490
/******************************************************************************
500491
* Dictionary

src/plugins_types.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -528,19 +528,19 @@ lyplg_type_validate_patterns(const struct ly_ctx *ctx, struct lysc_pattern **pat
528528
{
529529
LY_ERR r;
530530
LY_ARRAY_COUNT_TYPE u;
531-
pcre2_code *pcode;
531+
const pcre2_code *pcode;
532532

533533
LY_CHECK_ARG_RET(NULL, str, err, LY_EINVAL);
534534

535535
*err = NULL;
536536

537537
LY_ARRAY_FOR(patterns, u) {
538-
/* get the compiled pattern */
539-
LY_CHECK_RET(ly_ctx_get_pattern_code(ctx, patterns[u]->expr, &pcode));
538+
/* get (or compile) the compiled pattern. The pattern might not be found, because
539+
* if ctx is printed, it did not inherit compiled patterns from the original context. */
540+
LY_CHECK_RET(ly_ctx_get_or_compile_pattern_code(ctx, patterns[u]->expr, &pcode));
540541

541542
/* match the pattern */
542543
r = ly_pattern_code_match(pcode, str, str_len, err);
543-
pcre2_code_free(pcode);
544544
LY_CHECK_RET(r && (r != LY_ENOT), r);
545545

546546
if (((r == LY_ENOT) && !patterns[u]->inverted) || ((r == LY_SUCCESS) && patterns[u]->inverted)) {

src/schema_compile_node.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1388,7 +1388,7 @@ lys_compile_type_patterns(struct lysc_ctx *ctx, const struct lysp_restr *pattern
13881388
++(*pattern)->refcount;
13891389

13901390
/* compile and insert the pattern into the context hash table, if it wasnt already compiled */
1391-
LY_CHECK_GOTO(ret = ly_ctx_compile_and_cache_pattern_code(ctx->ctx, &patterns_p[u].arg.str[1]), done);
1391+
LY_CHECK_GOTO(ret = ly_ctx_get_or_compile_pattern_code(ctx->ctx, &patterns_p[u].arg.str[1], NULL), done);
13921392

13931393
if (patterns_p[u].arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
13941394
(*pattern)->inverted = 1;

src/tree_data_common.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1855,7 +1855,7 @@ ly_time_ts2str(const struct timespec *ts, char **str)
18551855
}
18561856

18571857
LY_ERR
1858-
ly_pattern_code_match(pcre2_code *pcode, const char *str, size_t str_len, struct ly_err_item **err)
1858+
ly_pattern_code_match(const pcre2_code *pcode, const char *str, size_t str_len, struct ly_err_item **err)
18591859
{
18601860
int r, match_opts;
18611861
pcre2_match_data *match_data = NULL;

src/tree_data_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,6 @@ void lyd_unlink_ignore_lyds(struct lyd_node **first_sibling, struct lyd_node *no
797797
* @return LY_ENOT if the string does not match;
798798
* @return LY_ERR on error.
799799
*/
800-
LY_ERR ly_pattern_code_match(pcre2_code *pcode, const char *str, size_t str_len, struct ly_err_item **err);
800+
LY_ERR ly_pattern_code_match(const pcre2_code *pcode, const char *str, size_t str_len, struct ly_err_item **err);
801801

802802
#endif /* LY_TREE_DATA_INTERNAL_H_ */

src/tree_schema_internal.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,7 @@ struct lysp_yin_ctx {
186186
*/
187187
struct ly_pattern_ht_rec {
188188
const char *pattern; /**< Pattern string, used both as key to hash and value to search for. */
189-
uint8_t *serialized_pattern; /**< Serialized pattern code for this pattern.
190-
Can be deserialized to a compiled pcre2_code that can be used directly,
191-
see ::ly_pcode_deserialize(). */
189+
pcre2_code *pcode; /**< Compiled PCRE2 pattern code. */
192190
};
193191

194192
/**

0 commit comments

Comments
 (0)