All of lore.kernel.org
 help / color / mirror / Atom feed
From: Mateusz Kusiak <mateusz.kusiak@intel.com>
To: linux-raid@vger.kernel.org
Cc: jes@trained-monkey.org
Subject: [PATCH] Utils: Change sprintf to snprintf
Date: Thu, 12 Aug 2021 13:48:48 +0200	[thread overview]
Message-ID: <20210812114848.17341-1-mateusz.kusiak@intel.com> (raw)

Using sprintf can cause segmentation fault by exceeding the size of buffer array.

Signed-off-by: Mateusz Kusiak <mateusz.kusiak@intel.com>
---
 util.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/util.c b/util.c
index cdf1da24..ea07277c 100644
--- a/util.c
+++ b/util.c
@@ -947,12 +947,12 @@ dev_t devnm2devid(char *devnm)
 	/* First look in /sys/block/$DEVNM/dev for %d:%d
 	 * If that fails, try parsing out a number
 	 */
-	char path[100];
+	char path[PATH_MAX];
 	char *ep;
 	int fd;
 	int mjr,mnr;
 
-	sprintf(path, "/sys/block/%s/dev", devnm);
+	snprintf(path, sizeof(path), "/sys/block/%s/dev", devnm);
 	fd = open(path, O_RDONLY);
 	if (fd >= 0) {
 		char buf[20];
-- 
2.26.2


             reply	other threads:[~2021-08-12  9:38 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-12 11:48 Mateusz Kusiak [this message]
2021-08-13 19:21 ` [PATCH] Utils: Change sprintf to snprintf Jes Sorensen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210812114848.17341-1-mateusz.kusiak@intel.com \
    --to=mateusz.kusiak@intel.com \
    --cc=jes@trained-monkey.org \
    --cc=linux-raid@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.