util/async: avoid NULL pointer dereference
PATCH 1/1 util/async: avoid NULL pointer dereference
if laio_init create linux_aio failed and return NULL, NULL pointer dereference will occur when laio_attach_aio_context dereference linux_aio in aio_get_linux_aio, so add assert to avoid it.
--- util/async.c | 1 + 1 file changed, 1 insertion(+)
util/async.c +1 -0
@@ -327,6 +327,7 @@ LinuxAioState *aio_get_linux_aio(AioContext *ctx) {if (!ctx->linux_aio) { ctx->linux_aio =laio_init (); +assert (ctx->linux_aio);laio_attach_aio_context (ctx->linux_aio, ctx); }return ctx->linux_aio;
Signature · 1 line
2.15.0.windows.1
Quoted 15 lines · Jie Wang
if
laio_initcreatelinux_aiofailed and return NULL, NULL pointer dereference will occur whenlaio_attach_aio_contextdereferencelinux_aioinaio_get_linux_aio, so add assert to avoid it.Signed-off-by Jie Wang <[email protected]>--- util/async.c | 1 + 1 file changed, 1 insertion(+)util/async.c +0
-0
util/async.c LinuxAioState *aio_get_linux_aio(AioContext *ctx) · line 333
Quoted 7 lines
util/async.c +1
-0@@ -327,6 +327,7 @@ LinuxAioState *aio_get_linux_aio(AioContext *ctx) {if (!ctx->linux_aio) { ctx->linux_aio =laio_init (); +assert (ctx->linux_aio);laio_attach_aio_context (ctx->linux_aio, ctx); }return ctx->linux_aio;
I'm afraid this is not the correct fix. If laio_init() can fail, this function should skip laio_attach_aio_context() and return NULL, then callers should check the return value and handle the error. E.g. Set s->use_linux_aio to false and fall back to posix I/O, and perhaps report the error with error_report. Or even better, call laio_init during raw_open and use error_setg(errp, ...).
assert() will simply crash the program, it is not the right way to catch errors.
Fam