Skip to content

data REFACTOR build strings with measured, bounded appends - #2625

Open
aravindor wants to merge 1 commit into
CESNET:develfrom
aravindor:refactor-bounded-strings
Open

aravindor wants to merge 1 commit into
CESNET:develfrom
aravindor:refactor-bounded-strings

Conversation

@aravindor

Copy link
Copy Markdown

The XPath string cast, concat() and the JSON predicate builder, lyd_path(), the LYB feature check and the CBOR number conversion grow strings by summing each length by hand and then writing with sprintf() / strcpy() / strcat(). Every one of those sums has to keep matching its format string, and these paths format values that come from parsed data. They are all correct today; this removes the hand-sized math so it cannot drift, the same way #2591 did for the type plugins.

  • xpath.c: new cast_string_append() measures the text with vsnprintf() and writes it bounded, keeping the step growth of cast_string_realloc(). concat() and the predicate builder use the existing ly_append_str().
  • tree_data.c: new lyd_path_str_append() does the same on top of lyd_path_str_enlarge(), so a static buffer still returns LY_EINCOMPLETE. The position predicate no longer needs a temporary asprintf().
  • parser_lyb.c: the enabled-feature lists use ly_append_str(). This also fixes lyb_check_mod_features() returning LY_SUCCESS when allocating the error message failed, which accepted data with mismatched features. The revision string uses snprintf().
  • parser_cbor.c, parser_common.c: snprintf() bounded by the allocation, and strdup() in place of malloc() + strcpy().

Output is unchanged. Built on devel with -fsanitize=address,undefined; the full test suite passes (71/71), no sanitizer reports, and make format-check is clean.

The XPath string cast, concat() and the JSON predicate, lyd_path(), the LYB
feature check and the CBOR number conversion grew strings by summing each
length by hand and writing with sprintf()/strcpy()/strcat(). The sums are
correct today but each one has to keep matching its format string, and these
paths format values that come from parsed data. Measure the formatted output
with vsnprintf() and write it bounded instead, as CESNET#2591 did for the type
plugins. Output is unchanged.

 - xpath.c: new cast_string_append() keeps the step growth of
   cast_string_realloc(); concat() and the predicate builder use
   ly_append_str().
 - tree_data.c: new lyd_path_str_append() on top of lyd_path_str_enlarge(),
   so static buffers still return LY_EINCOMPLETE.
 - parser_lyb.c: feature lists use ly_append_str(); the revision string uses
   snprintf(). This also fixes lyb_check_mod_features() returning LY_SUCCESS
   when allocating the error message failed, which accepted mismatched
   features.
 - parser_cbor.c, parser_common.c: snprintf() bounded by the allocation,
   strdup() instead of malloc()+strcpy().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant