libxl: handle null lists in libxl_string_list_length
Quoted 8 lines · ----- [email protected] wrote:
After commit
b0be2b12("libxl: fixlibxl_string_list_lengthand its only
caller")libxl_string_list_lengthno longer handles null (empty) lists. Fix
so they are handled, returning length 0.While at it, remove the unneccessary undereferenced null pointer check
Are you sure this check should be removed? This routine can be called from anywhere (at least within libxl it seems) and one day someone will call it with NULL argument.
I'd probably leave this check in.
-boris
Quoted 32 lines
and tidy the layout of the function.
Reported-by Boris Ostrovsky <[email protected]>Signed-off-by Matthew Daley <[email protected]>---
I've verified that this fixes the no-bootloader-arguments case.tools/libxl/libxl.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)tools/libxl/libxl.c +5
-2@@ -200,9 +200,12 @@ void libxl_string_list_dispose(libxl_string_list *psl)int libxl_string_list_length (const libxl_string_list *psl) { -if (!psl)return 0 ;int i =0 ; -while ((*psl)[i]) i++; + +if (*psl) +while ((*psl)[i]) + i++; +return i; }Signature · 2 lines
-- 1.7.10.4
Quoted 9 lines · Boris Ostrovsky
Quoted 6 lines · ----- [email protected] wrote:
After commit
b0be2b12("libxl: fixlibxl_string_list_lengthand its only
caller")libxl_string_list_lengthno longer handles null (empty) lists. Fix
so they are handled, returning length 0.
Quoted 8 lines
Quoted 2 lines
While at it, remove the unneccessary undereferenced null pointer check
Are you sure this check should be removed? This routine can be called from anywhere (at least within libxl it seems) and one day someone will call it with NULL argument.
I'd probably leave this check in.
I would argue that any such invocation would be an error by the caller and should fail noisily, similar to how passing NULL into strlen should not return 0. libxl_{string,key_value}_list_dispose similarly assumes non-NULL pointers, FWIW.
Ian C., do you have an opinion either way?
- Matthew
Quoted 17 lines · Matthew Daley
Quoted 8 lines · Boris Ostrovsky
Quoted 6 lines · ----- [email protected] wrote:
After commit
b0be2b12("libxl: fixlibxl_string_list_lengthand its only
caller")libxl_string_list_lengthno longer handles null (empty) lists. Fix
so they are handled, returning length 0.Quoted 7 lines
Quoted 2 lines
While at it, remove the unneccessary undereferenced null pointer check
Are you sure this check should be removed? This routine can be called from anywhere (at least within libxl it seems) and one day someone will call it with NULL argument.
I'd probably leave this check in.
Quoted 8 lines
I would argue that any such invocation would be an error by the caller and should fail noisily, similar to how passing NULL into strlen should not return 0. libxl_{string,key_value}_list_dispose similarly assumes non-NULL pointers, FWIW.
Ian C., do you have an opinion either way?
- Matthew
I would agree that any passing of NULL is a caller error. Possibly an explicit check and abort()? If it is going to be noisy, we should be nice and help out the debugger.
~Andrew
Quoted 18 lines · Matthew Daley
Quoted 16 lines · Boris Ostrovsky
Quoted 8 lines · ----- [email protected] wrote:
After commit
b0be2b12("libxl: fixlibxl_string_list_lengthand its only
caller")libxl_string_list_lengthno longer handles null (empty) lists. Fix
so they are handled, returning length 0.While at it, remove the unneccessary undereferenced null pointer check
Are you sure this check should be removed? This routine can be called from anywhere (at least within libxl it seems) and one day someone will call it with NULL argument.
Quoted 8 lines
Quoted 1 line
I'd probably leave this check in.
I would argue that any such invocation would be an error by the caller and should fail noisily, similar to how passing NULL into strlen should not return 0. libxl_{string,key_value}_list_dispose similarly assumes non-NULL pointers, FWIW.
Ian C., do you have an opinion either way?
I think a zero length list is a bit different to a NULL string and should return 0.
But libxl_string_list is already char** so this function is taking char***. The check for char *** == NULL, which is being removed, appears to be unnecessary. A zero length list would be char ** == NULL, which should be handled (and is I think?). char * == NULL would be a "" entry in the string list.
Confused? I know I am ;-)
Ian.
Quoted 26 lines · Ian Campbell
Quoted 25 lines · Matthew Daley
Quoted 9 lines · Boris Ostrovsky
Quoted 6 lines · ----- [email protected] wrote:
After commit
b0be2b12("libxl: fixlibxl_string_list_lengthand its only
caller")libxl_string_list_lengthno longer handles null (empty) lists. Fix
so they are handled, returning length 0.Quoted 8 lines
Quoted 2 lines
While at it, remove the unneccessary undereferenced null pointer check
Are you sure this check should be removed? This routine can be called from anywhere (at least within libxl it seems) and one day someone will call it with NULL argument.
I'd probably leave this check in.
I would argue that any such invocation would be an error by the caller and should fail noisily, similar to how passing NULL into strlen should not return 0. libxl_{string,key_value}_list_dispose similarly assumes non-NULL pointers, FWIW.
Quoted 4 lines
Quoted 1 line
Ian C., do you have an opinion either way?
I think a zero length list is a bit different to a NULL string and should return 0.
Perhaps it was a bad analogy, but passing NULL to this function isn't giving it an empty list, it's giving it no (NULL!) list. We don't check for null pointers everywhere else non-optional pointers are passed (at least, we shouldn't be, IMO...)
Quoted 6 lines
But
libxl_string_listis already char** so this function is taking char***. The check for char *** == NULL, which is being removed, appears to be unnecessary. A zero length list would be char ** == NULL, which should be handled (and is I think?). char * == NULL would be a "" entry in the string list.
This was my intention in this patch; only the char *** == NULL check is removed, and the char ** == NULL for empty lists is handled by the newly added if condition.
But char * == NULL doesn't mean an "" entry, doesn't it instead mark the end of the list (see xlu_cfg_get_list_as_string_list for example)? This is currently being checked for in the while loop condition. To continue using your notation, instead char == NULL is an empty string value in the list.
Quoted 2 lines
Confused? I know I am ;-)
:)
- Matthew
Quoted 3 lines
Ian.
Quoted 24 lines · Matthew Daley
Quoted 23 lines · Ian Campbell
Quoted 22 lines · Matthew Daley
Quoted 8 lines · Boris Ostrovsky
Quoted 6 lines · ----- [email protected] wrote:
After commit
b0be2b12("libxl: fixlibxl_string_list_lengthand its only
caller")libxl_string_list_lengthno longer handles null (empty) lists. Fix
so they are handled, returning length 0.Quoted 7 lines
Quoted 2 lines
While at it, remove the unneccessary undereferenced null pointer check
Are you sure this check should be removed? This routine can be called from anywhere (at least within libxl it seems) and one day someone will call it with NULL argument.
I'd probably leave this check in.
I would argue that any such invocation would be an error by the caller and should fail noisily, similar to how passing NULL into strlen should not return 0. libxl_{string,key_value}_list_dispose similarly assumes non-NULL pointers, FWIW.
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Ian C., do you have an opinion either way?
I think a zero length list is a bit different to a NULL string and should return 0.
Perhaps it was a bad analogy, but passing NULL to this function isn't giving it an empty list, it's giving it no (NULL!) list. We don't check for null pointers everywhere else non-optional pointers are passed (at least, we shouldn't be, IMO...)
What if someone assigns 'libxl_string_list *psl = NULL' if, say, main()'s argc is 1 (i.e. there is no argument list) and then, later, calls libxl_string_list_length(psl) to find out whether something needs to be allocated for the list. Isn't getting a zero back an expected answer?
(I am afraid we are approaching rathole territory here.)
-boris
Quoted 23 lines
Quoted 6 lines
But
libxl_string_listis already char** so this function is taking char***. The check for char *** == NULL, which is being removed, appears to be unnecessary. A zero length list would be char ** == NULL, which should be handled (and is I think?). char * == NULL would be a "" entry in the string list.This was my intention in this patch; only the char *** == NULL check is removed, and the char ** == NULL for empty lists is handled by the newly added if condition.
But char * == NULL doesn't mean an "" entry, doesn't it instead mark the end of the list (see
xlu_cfg_get_list_as_string_listfor example)? This is currently being checked for in the while loop condition. To continue using your notation, instead char == NULL is an empty string value in the list.Quoted 1 line
Confused? I know I am ;-)
:)
- Matthew
Quoted 2 lines
Ian.
Quoted 33 lines · Boris Ostrovsky
Quoted 24 lines · Matthew Daley
Quoted 23 lines · Ian Campbell
Quoted 22 lines · Matthew Daley
Quoted 8 lines · Boris Ostrovsky
Quoted 6 lines · ----- [email protected] wrote:
After commit
b0be2b12("libxl: fixlibxl_string_list_lengthand its only
caller")libxl_string_list_lengthno longer handles null (empty) lists. Fix
so they are handled, returning length 0.Quoted 7 lines
Quoted 2 lines
While at it, remove the unneccessary undereferenced null pointer check
Are you sure this check should be removed? This routine can be called from anywhere (at least within libxl it seems) and one day someone will call it with NULL argument.
I'd probably leave this check in.
I would argue that any such invocation would be an error by the caller and should fail noisily, similar to how passing NULL into strlen should not return 0. libxl_{string,key_value}_list_dispose similarly assumes non-NULL pointers, FWIW.
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Ian C., do you have an opinion either way?
I think a zero length list is a bit different to a NULL string and should return 0.
Perhaps it was a bad analogy, but passing NULL to this function isn't giving it an empty list, it's giving it no (NULL!) list. We don't check for null pointers everywhere else non-optional pointers are passed (at least, we shouldn't be, IMO...)
Quoted 5 lines
What if someone assigns '
libxl_string_list*psl = NULL' if, say, main()'s argc is 1 (i.e. there is no argument list) and then, later, callslibxl_string_list_length(psl)to find out whether something needs to be allocated for the list. Isn't getting a zero back an expected answer?
This is the distinction Matthew are I were trying to make. In the case you describe you would do libxl_string_list psl = NULL (a zero length argument list) and call libxl_string_list_length(&psl).
libxl_string_list_length(NULL) is not asking for the length of a zero-length, list, it's asking for the length of no-list at all. If this were floating point the answer would be NaN ;-) Instead we get a segfault...
Ian.