From eab7cd47ae21e09920cc17414df85ee2952236b7 Mon Sep 17 00:00:00 2001 From: Long Liu Date: Wed, 12 Dec 2018 07:19:17 +0000 Subject: [PATCH] dm: hw: Replace sprintf with snprintf Replace function sprintf with snprintf in device model Tracked-On: #2079 Signed-off-by: Long Liu Reviewed-by: Shuo A Liu Reviewed-by: Acked-by: Yu Wang --- devicemodel/hw/pci/ahci.c | 7 +++++-- devicemodel/hw/pci/npk.c | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/devicemodel/hw/pci/ahci.c b/devicemodel/hw/pci/ahci.c index e60f52cf6..946251118 100644 --- a/devicemodel/hw/pci/ahci.c +++ b/devicemodel/hw/pci/ahci.c @@ -2307,7 +2307,7 @@ pci_ahci_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts, int atapi) char bident[16]; struct blockif_ctxt *bctxt; struct pci_ahci_vdev *ahci_dev; - int ret, slots; + int ret, slots, rc; uint8_t p; MD5_CTX mdctx; u_char digest[16]; @@ -2378,9 +2378,12 @@ pci_ahci_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts, int atapi) MD5_Init(&mdctx); MD5_Update(&mdctx, opts, strlen(opts)); MD5_Final(digest, &mdctx); - sprintf(ahci_dev->port[p].ident, + rc = snprintf(ahci_dev->port[p].ident, + sizeof(ahci_dev->port[p].ident), "ACRN--%02X%02X-%02X%02X-%02X%02X", digest[0], digest[1], digest[2], digest[3], digest[4], digest[5]); + if (rc > sizeof(ahci_dev->port[p].ident)) + WPRINTF("%s: digest is longer than ident\n", __func__); /* * Allocate blockif request structures and add them diff --git a/devicemodel/hw/pci/npk.c b/devicemodel/hw/pci/npk.c index 7e28cad18..6660fc4a6 100644 --- a/devicemodel/hw/pci/npk.c +++ b/devicemodel/hw/pci/npk.c @@ -178,7 +178,7 @@ static inline int valid_param(uint32_t m_off, uint32_t m_num) */ static int pci_npk_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts) { - int i, b, s, f, fd, ret, error = -1; + int i, b, s, f, fd, ret, rc, error = -1; DIR *dir; struct dirent *dent; char name[PATH_MAX]; @@ -239,7 +239,10 @@ static int pci_npk_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts) } /* read the host NPK configuration space */ - sprintf(name, "%s/%s/config", NPK_DRV_SYSFS_PATH, dent->d_name); + rc = snprintf(name, PATH_MAX, "%s/%s/config", NPK_DRV_SYSFS_PATH, + dent->d_name); + if (rc > PATH_MAX) + WPRINTF(("NPK device name too long\n")); fd = open(name, O_RDONLY); if (fd == -1) { WPRINTF(("Cannot open host NPK config\n"));