Skip to content

nulstr: strv_make_nulstr() to return consistent size - #25394

Closed
qdeslandes wants to merge 1 commit into
systemd:mainfrom
qdeslandes:nulstr_consistent_size
Closed

qdeslandes wants to merge 1 commit into
systemd:mainfrom
qdeslandes:nulstr_consistent_size

Conversation

@qdeslandes

Copy link
Copy Markdown
Contributor

The previous behaviour of strv_make_nulstr was the following:

  • If strv is empty, allocate 2 bytes for an empty nulstr and return ret_size = 0.
  • If strv is not empty, allocate as many bytes as needed for the strings and return ret_size = allocated_size - 1.

This behaviour makes it hard to identify the actual size of the allocated buffer.

To solve this issue, this commit always uses two NULL bytes to mark the end of the nulstr, and set ret_size = allocated_size - 2. Hence, ret_size contains the actual size of the nulstr content. If the nulstr is not empty, it will end with three NULL characters: the first one would be part of the last string in the nulstr, while the two last ones would be the nulstr termination mark.

Some functions have the assumption that nulstr ends with two NULL characters, so this change keeps this behaviour.

The previous behaviour of strv_make_nulstr was the following:
- If strv is empty, allocate 2 bytes for an empty nulstr and return
  ret_size = 0.
- If strv is not empty, allocate as many bytes as needed for the strings
  and return ret_size = allocated_size - 1.

This behaviour makes it hard to identify the actual size of the allocated
buffer.

To solve this issue, this commit always uses two NULL bytes to mark the
end of the nulstr, and set ret_size = allocated_size - 2. Hence, ret_size
contains the actual size of the nulstr content. If the nulstr is not empty,
it will end with three NULL characters: the first one would be part of the
last string in the nulstr, while the two last ones would be the nulstr
termination mark.

Some functions have the assumption that nulstr ends with two NULL
characters, so this change keeps this behaviour.

@yuwata yuwata left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, ret_size is "the sum of strlen() of each string + number of string", and that is consistent even when the strv is empty. I cannot catch the difficulty you mentioned...

Comment thread src/basic/nulstr-util.c
} else {
/* Make sure there is a second extra NUL at the end of resulting nulstr (not counted in return size) */
m[n] = '\0';
m[n+1] = '\0';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not necessary, as in the above, string with trailing NUL is already copied. So, now we have three NUL at the end...

@yuwata yuwata added reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks util-lib labels Nov 15, 2022
@qdeslandes

Copy link
Copy Markdown
Contributor Author

Previously, ret_size is "the sum of strlen() of each string + number of string", and that is consistent even when the strv is empty. I cannot catch the difficulty you mentioned...

Depending was is written to nulstr, the actually allocated buffer is either ret_size + 1 (strv is was not empty) or ret_size + 2 (strv was empty). This is the inconsistency I'm talking about.

Have two NULL chars (three with the last string's NULL termination character, if any) ensure:

  • The allocated buffer size is always ret_size + 2.
  • strv_parse_nulstr(), NULSTR_FOREACH()... and other functions expected 2 NULL chars at the end of nulstr still work as expected, whether the nulstr is empty or not.

@yuwata

yuwata commented Nov 15, 2022 •

Copy link
Copy Markdown
Member

Then, how about to simply set *ret_size = 1; when strv is empty? As when strv is empty, the buffer contains only two NUL, that can be also considered as one empty string + terminating NUL. Though, IIRC, that is contradict with the motivation of the previous @poettering's PR.

@poettering

Copy link
Copy Markdown
Member

yeah, @yuwata is right. if we copy a string to the nulstr blob we already copy its final NUL byte along. So when we then add a final extra one, we know that there will be two NUL bytes at the end -- as long as at least one string was copied. With your patch you will then add yet another one, which would mean three NUL bytes in a series.

@yuwata

yuwata commented Nov 15, 2022 •

Copy link
Copy Markdown
Member

(BTW, I noticed that the function provides contradict results when strv is STRV_MAKE("") vs empty. I think both should provide the same result. Moreover, when we pass STRV_MAKE("foo", "", "bar"), the resulting nulstr is invalid...)

@qdeslandes

Copy link
Copy Markdown
Contributor Author

yeah, @yuwata is right. if we copy a string to the nulstr blob we already copy its final NUL byte along. So when we then add a final extra one, we know that there will be two NUL bytes at the end -- as long as at least one string was copied. With your patch you will then add yet another one, which would mean three NUL bytes in a series.

My reasoning is that we need to differentiate between the content of the nulstr, and the termination mark of the nulstr.

The empty nulstr should have 2 NUL characters, so that the assumption that we always have two trailing NULs holds, meaning 2 NUL characters represent the end of a nulstr. It doesn't make sense to set *ret_size = 1 because the length of the content of the empty nulstr is not 1, but 0.

Hence, a non-empty nulstr should also have 2 NUL characters at the end, to mark the end of the nulstr. The NUL character before those is part of the content.

(BTW, I noticed that the function provides contradict results when strv is STRV_MAKE("") vs empty. I think both should provide the same result.

Unless I'm mistaken, strv_make_nulstr() had this behaviour before my changes. I think it's reasonable: we don't filter out empty strings.

Moreover, when we pass STRV_MAKE("foo", "", "bar"), the resulting nulstr is invalid...)

What do you mean by "invalid"?

@yuwata

yuwata commented Nov 15, 2022

Copy link
Copy Markdown
Member

(BTW, I noticed that the function provides contradict results when strv is STRV_MAKE("") vs empty. I think both should provide the same result.

Unless I'm mistaken, strv_make_nulstr() had this behaviour before my changes. I think it's reasonable: we don't filter out empty strings.

Yeah, at least, this is not your fault. Sorry for the off-topic.

Moreover, when we pass STRV_MAKE("foo", "", "bar"), the resulting nulstr is invalid...)

What do you mean by "invalid"?

I mean, we cannot get "bar" from the resulting nulstr with NULSTR_FOREACH(). But, again, this is not your fault in this PR.

@qdeslandes

qdeslandes commented Nov 15, 2022 •

Copy link
Copy Markdown
Contributor Author

I mean, we cannot get "bar" from the resulting nulstr with NULSTR_FOREACH(). But, again, this is not your fault in this PR.

Agreed, we discussed this on Telegram, from my point of view this should be fixed. Apparently, it's expected behaviour.

@daandemeyer

Copy link
Copy Markdown
Collaborator

Agreed, we discussed this on Telegram, from my point of view this should be fixed. Apparently, it's expected behaviour.

Whether we should change our definition of nulstr to allow empty strings is another discussion altogether, let's not try to fix that in this PR. Let's just focus here on the size issue. We can always discuss whether we should fix the empty string thing in another PR

@poettering

Copy link
Copy Markdown
Member

(BTW, I noticed that the function provides contradict results when strv is STRV_MAKE("") vs empty. I think both should provide the same result. Moreover, when we pass STRV_MAKE("foo", "", "bar"), the resulting nulstr is invalid...)

I am pretty sure a nulstr with no entries and one with an empty stirng should be differently serialized and they are.

Note that nulstrs are used in to ways:

  1. a blob with a size. In which case an embedded empty string is fine. This is what strv_parse_nulstr() deals with. This is the format /proc/$PID/argv contains for example.
  2. a blob without a size. In which case an empty string is the end marker, and thus cannot be embedded. This is what strv_split_nulstr() and NULSTR_FOREACH() deal with. This we often use internally for path lists, where we know that we won#t have empty strings.

strv_make_nulstr() creates a blob that works for both uses. it returns a size which you can use for type #1 nulstrs, or you can ignore it, for type #2 nulstrs. To make this work this will append a NUL byte after the NUL byte of the last string, but not include it in the count.

@poettering

Copy link
Copy Markdown
Member

So I think we really shouldn't lost focus. The fact that there are two types of nulstrs sucks a bit, but I think there's reason to it. I think whenver we write nulstrs into files, or network messages or extended attributes we should go for type #1 nulstrs. And use type #2 nulstrs only when they originate from static const literal C strings where we know no embedded NUl bytes exist.

@qdeslandes qdeslandes closed this Dec 12, 2022
@qdeslandes
qdeslandes deleted the nulstr_consistent_size branch December 12, 2022 15:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks util-lib

Development

Successfully merging this pull request may close these issues.

4 participants