public inbox for linux-scsi@vger.kernel.org
 help / color / mirror / Atom feed
From: Pat LaVarre <p.lavarre@ieee.org>
To: dougg@torque.net
Cc: linux-scsi@vger.kernel.org
Subject: Re: sg_dd bpt= count=
Date: 20 Oct 2003 13:49:14 -0600	[thread overview]
Message-ID: <1066679354.2833.66.camel@patehci2> (raw)
In-Reply-To: <1066674092.2833.0.camel@patehci2>

> > sudo sg_dd of=/dev/sg0 if=/dev/zero bs=2k bpt= count=
> > may reliably take down kernels.

To sg3_utils sg_dd.c I first propose the following patch, to persuade
get_num to return determinate results more often.

Specifically I propose changing:

        char c;
        res = sscanf(buf, "%d%c", &num, &c);
        if (0 == res) ...
        else if (1 == res) ...
        else {
                switch (c) { ...

Personally I believe that source fragment switches on uninitialised c in
the situation `man sscanf` describes as: "RETURN VALUE ... The value EOF
is returned if an input failure occurs before any conversion such as an
end-of-file occurs ...".

As a test, I did separately execute get_num("").  For me once the
uninitialised c and num were then 8 and 1108545272 (aka x42130EF8), so
the result was -1.  I notice gcc -Wall doesn't mention this kind of
read-before-write.

Pat LaVarre

P.S. Also I wonder if we would prefer rewriting these "return -1" as
loud exits e.g.:

fprintf(stderr, "file %s line %d\n", __FILE__, __LINE__);
exit(-1);

--- sg3_utils-1.05/sg_dd.c	2003-10-19 03:35:32.000000000 -0600
+++ sg3_utils/sg_dd.c	2003-10-20 13:35:20.515143520 -0600
@@ -475,10 +475,10 @@
     char c;
 
     res = sscanf(buf, "%d%c", &num, &c);
-    if (0 == res)
-        return -1;
-    else if (1 == res)
+    if (1 == res)
         return num;
+    else if (2 != res)
+        return -1;
     else {
         switch (c) {
         case 'c':



  reply	other threads:[~2003-10-20 19:49 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-10-14 23:40 sg_dd bpt= count= Pat LaVarre
2003-10-20 18:21 ` Pat LaVarre
2003-10-20 19:49   ` Pat LaVarre [this message]
2003-10-20 21:34     ` Pat LaVarre
2003-10-20 23:53     ` Douglas Gilbert
2003-10-21 19:44       ` Pat LaVarre

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=1066679354.2833.66.camel@patehci2 \
    --to=p.lavarre@ieee.org \
    --cc=dougg@torque.net \
    --cc=linux-scsi@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox