Skip to content

Commit 61fb886

Browse files
committed
Fix -Werror=stringop-truncation errors
In the lib/vhd code: In function 'safe_strncpy', inlined from 'vhd_initialize_footer' at libvhd.c:2882:2: include/util.h:46:17: error: 'strncpy' output truncated before terminating nul copying 3 bytes from a string of the same length [-Werror=stringop-truncation] 46 | pdest = strncpy(dest, src, n - 1); In the lvm code twice: In function 'safe_strncpy', inlined from 'lvm_copy_name' at lvm-util.c:71:2, inlined from 'lvm_scan_lvs' at lvm-util.c:289:9: include/util.h:46:17: error: 'strncpy' output may be truncated copying 255 bytes from a string of length 255 [-Werror=stringop-truncation] 46 | pdest = strncpy(dest, src, n - 1); For VHD, just set the footer bytes individually and avoid the string ops. For LVM, switching back to strncpy and providing the full destination size silences the errors. strncpy does NUL terminate the string - it just may truncate. (safe_strncpy silences many other instances of stringop-truncation like: "error: 'strncpy' specified bound 1024 equals destination size", so we want to keep it.) Signed-off-by: Jason Andryuk <[email protected]>
1 parent be90010 commit 61fb886

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

lvm/lvm-util.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ lvm_copy_name(char *dst, const char *src, size_t size)
6868
if (strnlen(src, size) == size)
6969
return -ENAMETOOLONG;
7070

71-
safe_strncpy(dst, src, size);
71+
strncpy(dst, src, size);
7272
return 0;
7373
}
7474

@@ -102,7 +102,7 @@ lvm_parse_pv(struct vg *vg, const char *name, int pvs, uint64_t start)
102102
if (i == pvs)
103103
return -ENOMEM;
104104

105-
err = lvm_copy_name(pv->name, name, sizeof(pv->name) - 1);
105+
err = lvm_copy_name(pv->name, name, sizeof(pv->name));
106106
if (err)
107107
return err;
108108

@@ -286,7 +286,7 @@ lvm_scan_lvs(struct vg *vg)
286286
lv->segments = segs;
287287
lv->first_segment = seg;
288288

289-
err = lvm_copy_name(lv->name, name, sizeof(lv->name) - 1);
289+
err = lvm_copy_name(lv->name, name, sizeof(lv->name));
290290
if (err)
291291
goto out;
292292
err = -EINVAL;

vhd/lib/libvhd.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -2879,7 +2879,10 @@ vhd_initialize_footer(vhd_context_t *ctx, int type, uint64_t size)
28792879
ctx->footer.type = type;
28802880
ctx->footer.saved = 0;
28812881
ctx->footer.data_offset = 0xFFFFFFFFFFFFFFFFULL;
2882-
safe_strncpy(ctx->footer.crtr_app, "tap", sizeof(ctx->footer.crtr_app));
2882+
ctx->footer.crtr_app[0] = 't';
2883+
ctx->footer.crtr_app[1] = 'a';
2884+
ctx->footer.crtr_app[2] = 'p';
2885+
ctx->footer.crtr_app[3] = '\0';
28832886
uuid_generate(ctx->footer.uuid);
28842887
}
28852888

0 commit comments

Comments
 (0)