Skip to content

libubox: Drop extraneous space when appending values to variable#16

Closed
pprindeville wants to merge 1 commit into
openwrt:masterfrom
pprindeville:issue#15
Closed

libubox: Drop extraneous space when appending values to variable#16
pprindeville wants to merge 1 commit into
openwrt:masterfrom
pprindeville:issue#15

Conversation

@pprindeville

@pprindeville pprindeville commented Aug 4, 2024

Copy link
Copy Markdown
Member

If you (as someone extending this library) use __jshn_append() it will create a string with an undesired leading space.

fixes: issue #15

@pprindeville

Copy link
Copy Markdown
Member Author

Can we please get some progress on this?

@pprindeville

Copy link
Copy Markdown
Member Author

@hauke @nbd168

@BKPepe

BKPepe commented Jun 7, 2025

Copy link
Copy Markdown
Member

Robert (@robimarko) do you think thank you could help Philip to look at this? :)

@l-jonas

l-jonas commented Nov 5, 2025

Copy link
Copy Markdown

Could you describe a "test case" where this helps. This could be just a few lines of codes where the patch creates a difference. Just by looking at it, I don't understand it.

@pprindeville

Copy link
Copy Markdown
Member Author

Could you describe a "test case" where this helps. This could be just a few lines of codes where the patch creates a difference. Just by looking at it, I don't understand it.

If you do:

_jshn_append var 'foo'
_jshn_append var 'bar'

You'll end up with var=" foo bar" note the leading space.

@l-jonas

l-jonas commented Nov 11, 2025

Copy link
Copy Markdown

Functions starting with an underscore are internal functions. Are there paths to trigger the difference without using those that affect the final JSON?

@pprindeville

pprindeville commented Nov 11, 2025

Copy link
Copy Markdown
Member Author

Functions starting with an underscore are internal functions. Are there paths to trigger the difference without using those that affect the final JSON?

I don't understand the question. json_add_array and json_add_object call _json_add_table which calls _json_add_generic which calls _jshn_append.

One of the variables modified by _json_add_generic is K_${cur} which I use in my proposed changes (PR #6) when I try to get the root position or the index of the cursor inside an array or object.

@pprindeville

pprindeville commented Nov 11, 2025

Copy link
Copy Markdown
Member Author

This is trivial to demonstrate. And fix. It's a 3-line test and a 1 line fix.

root@OpenWrt:~# cat test.sh
#!/bin/sh

. /usr/share/libubox/jshn.sh

_jshn_append var 'foo'
echo "var='$var'"

root@OpenWrt:~# sh test.sh
var=' foo'
root@OpenWrt:~# 

This might be why we're not seeing new contributors sign up....

Don't have a leading space when building out a variable via appends.

fixes: issue openwrt#15

Signed-off-by: Felix Fietkau <nbd@nbd.name>
Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
@pprindeville pprindeville changed the title jshn.sh: Drop extraneous space when appending values to variable libubox: Drop extraneous space when appending values to variable Nov 12, 2025
@l-jonas

l-jonas commented Nov 12, 2025

Copy link
Copy Markdown

If it's trivial to demonstrate, then please demonstrate it. I am not asking for direct calls to this particular function as this is an internal function that should not be used directly. This "names that start with underscore are internal API only" is a common convention used in languages that have no or a limited concept of encapsulation.

So I am asking for a script that constructs a json object, json array or whatever using the public API only (functions not starting with an underscore) where you suggested change causes a difference.

This might be why we're not seeing new contributors sign up....

When contributing, I expect some quality assurance. If I can not explain why my change is an improvement, I don't expect anyone to accept it upstream. That's why the explanation of the change sometimes takes much more effort than the actual change. I don't know your expectations. This is also the other way around: If I see some project merging random stuff without reason, I can not trust that project anymore.

@pprindeville

pprindeville commented Nov 12, 2025

Copy link
Copy Markdown
Member Author

If it's trivial to demonstrate, then please demonstrate it. I am not asking for direct calls to this particular function as this is an internal function that should not be used directly. This "names that start with underscore are internal API only" is a common convention used in languages that have no or a limited concept of encapsulation.

So I am asking for a script that constructs a json object, json array or whatever using the public API only (functions not starting with an underscore) where you suggested change causes a difference.

I'm not saying it's publicly callable. But in writing additional functions (which are) that call through it, those functions didn't operate correctly because of the leading space interfering. This fix is a prerequisite to PR #6.

To reiterate, this bug was confirmed by @nbd168 and he provided the fix.

@l-jonas

l-jonas commented Nov 13, 2025

Copy link
Copy Markdown

So #6 is affected by this changes? Which difference does it create there and where/why?

@pprindeville

Copy link
Copy Markdown
Member Author

So #6 is affected by this changes? Which difference does it create there and where/why?

I had to copy and paste jshn_get_index into the DHCPv4 importer to get it to work with the packaged libubox.

@l-jonas

l-jonas commented Nov 13, 2025

Copy link
Copy Markdown

But this PR itself is not at all about jshn_get_index. So how is it related?

openwrt-bot pushed a commit that referenced this pull request Nov 13, 2025
Verifies the fix in commit 2065c3909935 ("libubox: Drop extraneous space
when appending values to variable") that prevents leading spaces when
appending to empty variables.

 test_jshn_append_no_leading_space
 ASSERT:expected:<foo> but was:< foo>
 ASSERT:expected:<first second> but was:< first second>

References: #16
Signed-off-by: Petr Štetiar <ynezz@true.cz>
@ynezz

ynezz commented Nov 13, 2025

Copy link
Copy Markdown
Member

This is trivial to demonstrate. And fix. It's a 3-line test and a 1 line fix.

I added such test 240d2d5, without this commit it results in https://github.com/ynezz/openwrt-libubox/actions/runs/19329020923/job/55287207923#step:9:30

Sorry for pushing it directly, I wanted to update this PR, PEBKAC error :) Thus closed via 240d2d5, thanks!

@ynezz ynezz closed this Nov 13, 2025
ynezz added a commit to ynezz/openwrt-libubox that referenced this pull request Nov 13, 2025
Add new test which verifies _jshn_append leading space fix by appending
keys to JSON objects, making sure there is no leading space on the first
key when adding second key.

Tests: openwrt#16
Signed-off-by: Petr Štetiar <ynezz@true.cz>
ynezz added a commit to ynezz/openwrt-libubox that referenced this pull request Nov 13, 2025
Add another test which verifies _jshn_append leading space fix in commit
82cb5fd ("libubox: Drop extraneous space when appending values to
variable") by appending keys to JSON objects, making sure there is no
leading space on the first key when adding second key.

  test_jshn_append_via_json_script
  ASSERT:expected:<first> but was:< first>
  ASSERT:expected:<first second> but was:< first second>

Tests: openwrt#16
Signed-off-by: Petr Štetiar <ynezz@true.cz>
@ynezz

ynezz commented Nov 13, 2025

Copy link
Copy Markdown
Member

So I am asking for a script that constructs a json object, json array or whatever using the public API only (functions not starting with an underscore) where you suggested change causes a difference.

@l-jonas FYI #30

@pprindeville
pprindeville deleted the issue#15 branch November 14, 2025 19:10
Noltari pushed a commit to ynezz/openwrt-libubox that referenced this pull request Nov 17, 2025
Add another test which verifies _jshn_append leading space fix in commit
82cb5fd ("libubox: Drop extraneous space when appending values to
variable") by appending keys to JSON objects, making sure there is no
leading space on the first key when adding second key.

  test_jshn_append_via_json_script
  ASSERT:expected:<first> but was:< first>
  ASSERT:expected:<first second> but was:< first second>

Tests: openwrt#16
Signed-off-by: Petr Štetiar <ynezz@true.cz>
Link: openwrt#30
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
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.

5 participants