dm: refine the check of return value of snprintf

int snprintf(char *str, size_t size, const char *format, ...)
The functions snprintf() write at most size bytes (including the
terminating null byte('\0')) to str.
only when returned value of snprintf is non-negative and less than size,
the string has been completely written.

Tracked-On: #4087
Signed-off-by: Gao Junhao <junhao.gao@intel.com>
Reviewed-by: Yonghua Huang <yonghua.huang@intel.com>
Acked-by: Yu Wang <yu1.wang@intel.com>
This commit is contained in:
Gao Junhao 2019-10-12 03:18:52 +00:00 committed by wenlingz
parent 720a77c190
commit 995efc1b6f
3 changed files with 9 additions and 9 deletions

View File

@ -2383,8 +2383,8 @@ pci_ahci_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts, int atapi)
sizeof(ahci_dev->port[p].ident), sizeof(ahci_dev->port[p].ident),
"ACRN--%02X%02X-%02X%02X-%02X%02X", digest[0], "ACRN--%02X%02X-%02X%02X-%02X%02X", digest[0],
digest[1], digest[2], digest[3], digest[4], digest[5]); digest[1], digest[2], digest[3], digest[4], digest[5]);
if (rc > sizeof(ahci_dev->port[p].ident)) if (rc >= sizeof(ahci_dev->port[p].ident) || rc < 0)
WPRINTF("%s: digest is longer than ident\n", __func__); WPRINTF("%s: digest number is invalid!\n", __func__);
/* /*
* Allocate blockif request structures and add them * Allocate blockif request structures and add them

View File

@ -243,8 +243,8 @@ static int pci_npk_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
/* read the host NPK configuration space */ /* read the host NPK configuration space */
rc = snprintf(name, PATH_MAX, "%s/%s/config", NPK_DRV_SYSFS_PATH, rc = snprintf(name, PATH_MAX, "%s/%s/config", NPK_DRV_SYSFS_PATH,
dent->d_name); dent->d_name);
if (rc > PATH_MAX) if (rc >= PATH_MAX || rc < 0)
WPRINTF(("NPK device name too long\n")); WPRINTF(("NPK device name is invalid!\n"));
closedir(dir); closedir(dir);
fd = open(name, O_RDONLY); fd = open(name, O_RDONLY);

View File

@ -427,12 +427,12 @@ virtio_blk_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
MD5_Init(&mdctx); MD5_Init(&mdctx);
MD5_Update(&mdctx, opts, strnlen(opts, VIRTIO_BLK_MAX_OPTS_LEN)); MD5_Update(&mdctx, opts, strnlen(opts, VIRTIO_BLK_MAX_OPTS_LEN));
MD5_Final(digest, &mdctx); MD5_Final(digest, &mdctx);
if (snprintf(blk->ident, sizeof(blk->ident), rc = snprintf(blk->ident, sizeof(blk->ident),
"ACRN--%02X%02X-%02X%02X-%02X%02X", digest[0], "ACRN--%02X%02X-%02X%02X-%02X%02X", digest[0],
digest[1], digest[2], digest[3], digest[4], digest[1], digest[2], digest[3], digest[4], digest[5]);
digest[5]) >= sizeof(blk->ident)) {
WPRINTF(("virtio_blk: block ident too long\n")); if (rc >= sizeof(blk->ident) || rc < 0)
} WPRINTF(("virtio_blk: block ident is invalid!\n"));
/* setup virtio block config space */ /* setup virtio block config space */
blk->cfg.capacity = size / DEV_BSIZE; /* 512-byte units */ blk->cfg.capacity = size / DEV_BSIZE; /* 512-byte units */