diff mbox series

[2/4] libvduse: Replace strcpy() with strncpy()

Message ID 20220627090203.87-3-xieyongji@bytedance.com
State New
Headers show
Series Fix some coverity issues on VDUSE | expand

Commit Message

Yongji Xie June 27, 2022, 9:02 a.m. UTC
Coverity reported a string overflow issue since we copied
"name" to "dev_config->name" without checking the length.
This should be a false positive since we already checked
the length of "name" in vduse_name_is_invalid(). But anyway,
let's replace strcpy() with strncpy() to fix the coverity
complaint.

Fixes: Coverity CID 1490224
Signed-off-by: Xie Yongji <xieyongji@bytedance.com>
---
 subprojects/libvduse/libvduse.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Richard Henderson June 28, 2022, 12:25 a.m. UTC | #1
On 6/27/22 14:32, Xie Yongji wrote:
> -    strcpy(dev_config->name, name);
> +    strncpy(dev_config->name, name, VDUSE_NAME_MAX);
> +    dev_config->name[VDUSE_NAME_MAX - 1] = '\0';

g_strlcpy

r~
Yongji Xie June 28, 2022, 2:59 a.m. UTC | #2
On Tue, Jun 28, 2022 at 8:26 AM Richard Henderson
<richard.henderson@linaro.org> wrote:
>
> On 6/27/22 14:32, Xie Yongji wrote:
> > -    strcpy(dev_config->name, name);
> > +    strncpy(dev_config->name, name, VDUSE_NAME_MAX);
> > +    dev_config->name[VDUSE_NAME_MAX - 1] = '\0';
>
> g_strlcpy
>

Now we don't have a dependency on glib, so we use strncpy here.

Thanks,
Yongji
Markus Armbruster June 29, 2022, 9:38 a.m. UTC | #3
Xie Yongji <xieyongji@bytedance.com> writes:

> Coverity reported a string overflow issue since we copied
> "name" to "dev_config->name" without checking the length.
> This should be a false positive since we already checked
> the length of "name" in vduse_name_is_invalid(). But anyway,
> let's replace strcpy() with strncpy() to fix the coverity
> complaint.

Mention why you can't use something nicer from GLib?

> Fixes: Coverity CID 1490224
> Signed-off-by: Xie Yongji <xieyongji@bytedance.com>

Reviewed-by: Markus Armbruster <armbru@redhat.com>
diff mbox series

Patch

diff --git a/subprojects/libvduse/libvduse.c b/subprojects/libvduse/libvduse.c
index 6374933881..1e36227388 100644
--- a/subprojects/libvduse/libvduse.c
+++ b/subprojects/libvduse/libvduse.c
@@ -1309,7 +1309,8 @@  VduseDev *vduse_dev_create(const char *name, uint32_t device_id,
         goto err_dev;
     }
 
-    strcpy(dev_config->name, name);
+    strncpy(dev_config->name, name, VDUSE_NAME_MAX);
+    dev_config->name[VDUSE_NAME_MAX - 1] = '\0';
     dev_config->device_id = device_id;
     dev_config->vendor_id = vendor_id;
     dev_config->features = features;