Repository navigation
nulstr: strv_make_nulstr() to return consistent size - #25394
qdeslandes wants to merge 1 commit into
Conversation
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
left a comment
There was a problem hiding this comment.
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...
| } 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'; |
There was a problem hiding this comment.
This is not necessary, as in the above, string with trailing NUL is already copied. So, now we have three NUL at the end...
Depending was is written to Have two NULL chars (three with the last string's NULL termination character, if any) ensure:
|
|
Then, how about to simply set |
|
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. |
|
(BTW, I noticed that the function provides contradict results when strv is |
My reasoning is that we need to differentiate between the content of the The empty Hence, a non-empty
Unless I'm mistaken,
What do you mean by "invalid"? |
Yeah, at least, this is not your fault. Sorry for the off-topic.
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. |
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 |
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:
strv_make_nulstr() creates a blob that works for both uses. it returns a size which you can use for type |
|
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 |
The previous behaviour of strv_make_nulstr was the following:
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.