tools: libxl: free devpath on failure in libxl__blktap_devpath
PATCH 1/1 tools: libxl: free devpath on failure in libxl__blktap_devpath
The underlying code paths in tap_ctl_create attempt to handle both *devpath == NULL (by allocating) and *devpath != NULL (caller provided name) and if they allocate tend to write the return immediately before doing other potentially error generating tasks. All of which makes handling this at a lower level rather more complicated than handling it in the error path of libxl__bllktap_devpath.
Note that libxl__blktap_devpath initialises devpath to NULL and if the earlier GCSPRINTF succeeds then the value is returned earlier. Therefore if we make it to the call to tap_ctl_create then devpath is still NULL on entry, therefore on the error path devpath is either still NULL or has been set to a freshly allocated value by tap_ctl_create. Since free(NULL) is fine it is sufficient to just free(devpath).
I also considered adding a non-NULL devnull to the gc, even on failure, but that would have required a comment to explain the apparently strange behaviour.
Compile tested only.
CID: 1215177
--- tools/libxl/libxl_blktap2.c | 1 + 1 file changed, 1 insertion(+)
tools/libxl/libxl_blktap2.c +1 -0
@@ -47,6 +47,7 @@ char *libxl__blktap_devpath(libxl__gc *gc,return devname; } +free (devname);return NULL ; }
Signature · 1 line
2.1.4
Quoted 7 lines · Ian Campbell
The underlying code paths in
tap_ctl_createattempt to handle both *devpath == NULL (by allocating) and *devpath != NULL (caller provided name) and if they allocate tend to write the return immediately before doing other potentially error generating tasks. All of which makes handling this at a lower level rather more complicated than handling it in the error path oflibxl__bllktap_devpath.
Extraneous "l" in the function name.
Quoted 12 lines
Note that
libxl__blktap_devpathinitialises devpath to NULL and if the earlier GCSPRINTF succeeds then the value is returned earlier. Therefore if we make it to the call totap_ctl_createthen devpath is still NULL on entry, therefore on the error path devpath is either still NULL or has been set to a freshly allocated value bytap_ctl_create. Since free(NULL) is fine it is sufficient to just free(devpath).I also considered adding a non-NULL devnull to the gc, even on failure, but that would have required a comment to explain the apparently strange behaviour.
Quoted 5 lines
Compile tested only.
CID: 1215177
Signed-off-by Ian Campbell <[email protected]>
Quoted 19 lines
--- tools/libxl/libxl_blktap2.c | 1 + 1 file changed, 1 insertion(+)tools/libxl/libxl_blktap2.c +1
-0@@ -47,6 +47,7 @@ char *libxl__blktap_devpath(libxl__gc *gc,return devname; } +free (devname);return NULL ; }Signature · 1 line
2.1.4
Wei Liu writes ("Re: [PATCH] tools: libxl: free devpath on failure in libxl__blktap_devpath"):
Quoted 7 lines · Ian Campbell
Quoted 6 lines
The underlying code paths in
tap_ctl_createattempt to handle both *devpath == NULL (by allocating) and *devpath != NULL (caller provided name) and if they allocate tend to write the return immediately before doing other potentially error generating tasks. All of which makes handling this at a lower level rather more complicated than handling it in the error path oflibxl__bllktap_devpath.
Quoted 1 line
Extraneous "l" in the function name.
Fixed this.
Thanks,
Ian.