All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Patterson <andrew.patterson@hp.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: "Mike Miller (OS Dev)" <mikem@beardog.cca.cpqcorp.net>,
	Jens Axboe <jens.axboe@oracle.com>,
	LKML <linux-kernel@vger.kernel.org>,
	LKML-SCSI <linux-scsi@vger.kernel.org>,
	mike.miller@hp.com
Subject: Re: [PATCH 1/1] cciss: resubmit export uid, model, vendor, rev to sysfs
Date: Fri, 10 Apr 2009 11:19:53 -0600	[thread overview]
Message-ID: <1239383993.5183.75.camel@grinch> (raw)
In-Reply-To: <20090409221722.6439ba11.akpm@linux-foundation.org>

On Thu, 2009-04-09 at 22:17 -0700, Andrew Morton wrote: 
> On Fri, 10 Apr 2009 05:08:54 +0000 Andrew Patterson <andrew.patterson@hp.com> wrote:
> 
> > > > +	char model[MODEL_LEN + 1];
> > > > ...
> > > > +		return snprintf(buf, MODEL_LEN + 2, "%s\n", drv->model);
> > > 
> > > Isn't the buffer sizing wrong here?  Should be MODEL_LEN+1.
> > > 
> > 
> > Don't we need space for the '\0' and the '\n'?
> 
> The second arg to snprintf() tells snprintf() how large the buffer is. 
> That buffer should be sized to allow room for the trailing \0.
> 
> So if MODEL_LEN represents the maximum number of characters in a string
> then you want:
> 
> 	char model[MODEL_LEN + 2];	/* Room for the \n and the \0 */
> 	...
> 	return snprintf(buf, sizeof(model), "%s\n", drv->model);

Note that I don't want the '\n" in the source string.  I just want it
output to the dest string (buf).  I could do:

  return snprintf(buf, sizeof(model) + 1, "%s\n", drv->model);

if that is preferable.  Perhaps what is confusing is using:

#define MODEL_LEN	16
char model[MODEL_LEN + 1];   /* SCSI model string */

So MODEL_LEN is not accounting for the '\0'.

I am not sure what the convention is here.  It have seen SCSI code that
uses the above, e.g., include/scsi/scsi_transport_sas.h.  But I am happy
to have *_LEN account for the '\0' if that makes it less confusing.

Andrew


      reply	other threads:[~2009-04-10 17:20 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-04-07 18:04 [PATCH 1/1] cciss: resubmit export uid, model, vendor, rev to sysfs Mike Miller (OS Dev)
2009-04-08  6:19 ` Jens Axboe
2009-04-08  8:13   ` Jens Axboe
2009-04-08 14:53     ` Mike Miller (OS Dev)
2009-04-08 12:26   ` Kay Sievers
2009-04-08 12:26     ` Kay Sievers
2009-04-08 16:24     ` Andrew Patterson
2009-04-08 16:24       ` Andrew Patterson
2009-04-08 16:34       ` Kay Sievers
2009-04-08 16:34         ` Kay Sievers
     [not found]     ` <1239258160.19984.217.camel@grinch>
     [not found]       ` <ac3eb2510904090719n730dededp54b25390a9087a79@mail.gmail.com>
     [not found]         ` <1239296221.19984.218.camel@grinch>
     [not found]           ` <ac3eb2510904091005q13e5b5a1j3907de22ad3df70c@mail.gmail.com>
2009-04-09 18:07             ` Andrew Patterson
2009-04-09 18:07               ` Andrew Patterson
2009-04-09 18:12               ` Kay Sievers
2009-04-09 18:12                 ` Kay Sievers
2009-04-10  4:52 ` Andrew Morton
2009-04-10  5:08   ` Andrew Patterson
2009-04-10  5:17     ` Andrew Morton
2009-04-10 17:19       ` Andrew Patterson [this message]

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=1239383993.5183.75.camel@grinch \
    --to=andrew.patterson@hp.com \
    --cc=akpm@linux-foundation.org \
    --cc=jens.axboe@oracle.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=mike.miller@hp.com \
    --cc=mikem@beardog.cca.cpqcorp.net \
    /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.