diff options
author | John Levon <john.levon@nutanix.com> | 2021-04-15 10:42:07 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-15 10:42:07 +0100 |
commit | f7e9ff4496cc53caf8217b6210e29628ec60561d (patch) | |
tree | c3f652ca18b02bde8bfae80c62f386f70ed880bf | |
parent | 2e50c5667ce8724b0b7963255d3016efbb1f568a (diff) | |
download | libvfio-user-f7e9ff4496cc53caf8217b6210e29628ec60561d.zip libvfio-user-f7e9ff4496cc53caf8217b6210e29628ec60561d.tar.gz libvfio-user-f7e9ff4496cc53caf8217b6210e29628ec60561d.tar.bz2 |
vfu_ctx_create(): validate flags argument (#442)
In addition, return ENOTSUP for unknown device types, and add some unit tests.
Signed-off-by: John Levon <john.levon@nutanix.com>
Reviewed-by: Thanos Makatos <thanos.makatos@nutanix.com>
-rw-r--r-- | include/libvfio-user.h | 2 | ||||
-rw-r--r-- | lib/libvfio-user.c | 6 | ||||
-rw-r--r-- | test/unit-tests.c | 15 |
3 files changed, 17 insertions, 6 deletions
diff --git a/include/libvfio-user.h b/include/libvfio-user.h index d1c30e4..a5dcc75 100644 --- a/include/libvfio-user.h +++ b/include/libvfio-user.h @@ -95,7 +95,7 @@ typedef enum { * * @trans: transport type * @path: path to socket file. - * @flags: context flags + * @flags: context flags (LIBVFIO_USER_FLAG_*) * @pvt: private data * @dev_type: device type * diff --git a/lib/libvfio-user.c b/lib/libvfio-user.c index e6b79f2..03ef6da 100644 --- a/lib/libvfio-user.c +++ b/lib/libvfio-user.c @@ -1187,14 +1187,16 @@ vfu_create_ctx(vfu_trans_t trans, const char *path, int flags, void *pvt, int err = 0; size_t i; - //FIXME: Validate arguments. + if ((flags & ~(LIBVFIO_USER_FLAG_ATTACH_NB)) != 0) { + return ERROR_PTR(EINVAL); + } if (trans != VFU_TRANS_SOCK) { return ERROR_PTR(ENOTSUP); } if (dev_type != VFU_DEV_TYPE_PCI) { - return ERROR_PTR(EINVAL); + return ERROR_PTR(ENOTSUP); } vfu_ctx = calloc(1, sizeof(vfu_ctx_t)); diff --git a/test/unit-tests.c b/test/unit-tests.c index 5ee3358..7f03fb7 100644 --- a/test/unit-tests.c +++ b/test/unit-tests.c @@ -699,9 +699,6 @@ test_get_region_info(UNUSED void **state) /* FIXME add check for multiple sparse areas */ } -/* - * FIXME expand and validate - */ static void test_vfu_ctx_create(void **state UNUSED) { @@ -711,6 +708,18 @@ test_vfu_ctx_create(void **state UNUSED) pm.hdr.id = PCI_CAP_ID_PM; pm.pmcs.nsfrst = 0x1; + vfu_ctx = vfu_create_ctx(VFU_TRANS_SOCK + 1, "", 0, NULL, VFU_DEV_TYPE_PCI); + assert_null(vfu_ctx); + assert_int_equal(ENOTSUP, errno); + + vfu_ctx = vfu_create_ctx(VFU_TRANS_SOCK, "", 0, NULL, VFU_DEV_TYPE_PCI + 4); + assert_null(vfu_ctx); + assert_int_equal(ENOTSUP, errno); + + vfu_ctx = vfu_create_ctx(VFU_TRANS_SOCK, "", 999, NULL, VFU_DEV_TYPE_PCI); + assert_null(vfu_ctx); + assert_int_equal(EINVAL, errno); + vfu_ctx = vfu_create_ctx(VFU_TRANS_SOCK, "", LIBVFIO_USER_FLAG_ATTACH_NB, NULL, VFU_DEV_TYPE_PCI); assert_non_null(vfu_ctx); |