Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1 | From bc87af1314325b00c6ac002a60a2b0f0caa81e34 Mon Sep 17 00:00:00 2001 |
| 2 | From: Xiao Ni <xni@redhat.com> |
| 3 | Date: Sat, 18 Mar 2017 10:33:44 +0800 |
| 4 | Subject: [PATCH 3/5] Replace snprintf with strncpy at some places to avoid |
| 5 | truncation |
| 6 | |
| 7 | In gcc7 there are some building errors like: |
| 8 | directive output may be truncated writing up to 31 bytes into a region of size 24 |
| 9 | snprintf(str, MPB_SIG_LEN, %s, mpb->sig); |
| 10 | |
| 11 | It just need to copy one string to target. So use strncpy to replace it. |
| 12 | |
| 13 | For this line code: snprintf(str, MPB_SIG_LEN, %s, mpb->sig); |
| 14 | Because mpb->sig has the content of version after magic, so |
| 15 | it's better to use strncpy to replace snprintf too. |
| 16 | |
| 17 | Signed-off-by: Xiao Ni <xni@redhat.com> |
| 18 | Signed-off-by: Jes Sorensen <Jes.Sorensen@gmail.com> |
| 19 | --- |
| 20 | Upstream-Status: Backport |
| 21 | super-intel.c | 9 ++++++--- |
| 22 | 1 file changed, 6 insertions(+), 3 deletions(-) |
| 23 | |
| 24 | diff --git a/super-intel.c b/super-intel.c |
| 25 | index 57c7e75..5499098 100644 |
| 26 | --- a/super-intel.c |
| 27 | +++ b/super-intel.c |
| 28 | @@ -1811,7 +1811,8 @@ static void examine_super_imsm(struct supertype *st, char *homehost) |
| 29 | __u32 reserved = imsm_reserved_sectors(super, super->disks); |
| 30 | struct dl *dl; |
| 31 | |
| 32 | - snprintf(str, MPB_SIG_LEN, "%s", mpb->sig); |
| 33 | + strncpy(str, (char *)mpb->sig, MPB_SIG_LEN); |
| 34 | + str[MPB_SIG_LEN-1] = '\0'; |
| 35 | printf(" Magic : %s\n", str); |
| 36 | snprintf(str, strlen(MPB_VERSION_RAID0), "%s", get_imsm_version(mpb)); |
| 37 | printf(" Version : %s\n", get_imsm_version(mpb)); |
| 38 | @@ -7142,14 +7143,16 @@ static int update_subarray_imsm(struct supertype *st, char *subarray, |
| 39 | |
| 40 | u->type = update_rename_array; |
| 41 | u->dev_idx = vol; |
| 42 | - snprintf((char *) u->name, MAX_RAID_SERIAL_LEN, "%s", name); |
| 43 | + strncpy((char *) u->name, name, MAX_RAID_SERIAL_LEN); |
| 44 | + u->name[MAX_RAID_SERIAL_LEN-1] = '\0'; |
| 45 | append_metadata_update(st, u, sizeof(*u)); |
| 46 | } else { |
| 47 | struct imsm_dev *dev; |
| 48 | int i; |
| 49 | |
| 50 | dev = get_imsm_dev(super, vol); |
| 51 | - snprintf((char *) dev->volume, MAX_RAID_SERIAL_LEN, "%s", name); |
| 52 | + strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN); |
| 53 | + dev->volume[MAX_RAID_SERIAL_LEN-1] = '\0'; |
| 54 | for (i = 0; i < mpb->num_raid_devs; i++) { |
| 55 | dev = get_imsm_dev(super, i); |
| 56 | handle_missing(super, dev); |
| 57 | -- |
| 58 | 2.12.2 |
| 59 | |