Skip to contentSkip to conversation

libxl: handle NULL in libxl__enum_from_string

PATCH In review Wei Liu 3 participants 6 messages Oct 13, 2017

lore
Share & tools
lore

Wei Liu

author
#0 lore raw

PATCH 1/1 libxl: handle NULL in libxl__enum_from_string

Discovered by Coverity.

---
Cc: Ian Jackson <[email protected]>

---
 tools/libxl/libxl_utils.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/libxl/libxl_utils.c +1 -1
index 507ee56c7c..9433693e72 100644
@@ -1017,7 +1017,7 @@ int libxl_get_max_nodes(libxl_ctx *ctx)
 int libxl__enum_from_string(const libxl_enum_string_table *t,
                             const char *s, int *e)
 {
-    if (!t) return ERROR_INVAL;
+    if (!t || !s) return ERROR_INVAL;
 
     for( ; t->s; t++) {
         if (!strcasecmp(t->s, s)) {
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#1 lore raw

Wei Liu writes ("[PATCH for-4.10] libxl: handle NULL in libxl__enum_from_string"):

Quoted 1 line

Discovered by Coverity.

But. Surely it is very wrong

tools/libxl/libxl_utils.c int libxl_get_max_nodes(libxl_ctx *ctx) · line 1020

Quoted 6 lines
tools/libxl/libxl_utils.c +1 -1
@@ -1017,7 +1017,7 @@ int libxl_get_max_nodes(libxl_ctx *ctx)
 int libxl__enum_from_string(const libxl_enum_string_table *t,
                             const char *s, int *e)
 {
-    if (!t) return ERROR_INVAL;
+    if (!t || !s) return ERROR_INVAL;

to call this function with s==NULL.

I'm not generally in favour of turning such mistakes from easy-to-debug crashes into hard-to-track-down error codes (especially with our nonspecific error codes).

Where does that occur ?

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

author
#2 lore raw
Quoted 14 lines · Ian Jackson

Wei Liu writes ("[PATCH for-4.10] libxl: handle NULL in libxl__enum_from_string"):

Quoted 1 line

Discovered by Coverity.

But. Surely it is very wrong

tools/libxl/libxl_utils.c int libxl_get_max_nodes(libxl_ctx *ctx) · line 1020

Quoted 6 lines
tools/libxl/libxl_utils.c +1 -1
@@ -1017,7 +1017,7 @@ int libxl_get_max_nodes(libxl_ctx *ctx)
 int libxl__enum_from_string(const libxl_enum_string_table *t,
                             const char *s, int *e)
 {
-    if (!t) return ERROR_INVAL;
+    if (!t || !s) return ERROR_INVAL;

to call this function with s==NULL.

Quoted 6 lines

I'm not generally in favour of turning such mistakes from easy-to-debug crashes into hard-to-track-down error codes (especially with our nonspecific error codes).

Where does that occur ?

libxl_*_type_from_string.

I agree they shouldn't be called with NULL. We should guard against error (here or the libxl_*_type_from_string) or annotate the input can't be NULL.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#3 lore raw

Wei Liu writes ("Re: [PATCH for-4.10] libxl: handle NULL in libxl__enum_from_string"):

Quoted 3 lines

I agree they shouldn't be called with NULL. We should guard against error (here or the libxl_*_type_from_string) or annotate the input can't be NULL.

I mean, who calls any libxl_*_from_string with s==NULL ?

Also I don't think we should annotate that the input value can't be NULL, especially in a situation like this where the semantics could only be "this is wrong". The API (and the internal calling conventions) are full of functions taking pointer arguments - are we going to annotate each one of those to say that it cannot be NULL ?

Instead, what we have actually done so far, is annotate when a pointer parameter *may* be NULL, and, in that case, what that means:

* If ao_how==NULL, the function will be synchronous.
* If ao_how->callback==NULL, a libxl_event will be generated which /* if old_name is NULL, any old name is OK; otherwise we check
/* May be called with info_r == NULL to check for domain's existence.
* event_occurs may be NULL if mask is 0.
* disaster may be NULL. If it is, or if _register_callbacks has
* The application may call beforepoll with fds==NULL and
_hidden void libxl__ptr_add(libxl__gc *gc_opt, void *ptr /* may be * NULL */) NN1; _hidden char *libxl__strdup(libxl__gc *gc_opt,

                            const char *c /* may be NULL */) NN1;

etc. etc.

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#4 lore raw
Quoted 6 lines · Ian Jackson

Wei Liu writes ("Re: [PATCH for-4.10] libxl: handle NULL in libxl__enum_from_string"):

Quoted 3 lines

I agree they shouldn't be called with NULL. We should guard against error (here or the libxl_*_type_from_string) or annotate the input can't be NULL.

I mean, who calls any libxl_*_from_string with s==NULL ?

Quoted 8 lines

Also I don't think we should annotate that the input value can't be NULL, especially in a situation like this where the semantics could only be "this is wrong". The API (and the internal calling conventions) are full of functions taking pointer arguments - are we going to annotate each one of those to say that it cannot be NULL ?

Instead, what we have actually done so far, is annotate when a pointer parameter *may* be NULL, and, in that case, what that means:

This is exactly what attribute nonnull exists for.  As a bonus, using the attribute will have the compiler complain at you if it spots a way NULL gets passed, and UBSAN will add specific instrumentation to check.

Alternatively, you could assert(s) which would catch all (ab)uses and also quiesce Coverity.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#5 lore raw

Andrew Cooper writes ("Re: [Xen-devel] [PATCH for-4.10] libxl: handle NULL in libxl__enum_from_string"):

Quoted 7 lines · Ian Jackson
Quoted 2 lines

Instead, what we have actually done so far, is annotate when a pointer parameter *may* be NULL, and, in that case, what that means:

This is exactly what attribute nonnull exists for.  As a bonus, using the attribute will have the compiler complain at you if it spots a way NULL gets passed, and UBSAN will add specific instrumentation to check.

Thanks for that excellent suggestion, which I ought to have thought of myself.

I'd be quite happy with patches to add the nonnull attribute to the parameters. We already have that for a number of the *alloc* functions - git-grep libxl for "NN1".

I don't mind the idea of adding that to some more functions now, even if we don't have complete coverage.

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel