lkml.org 
[lkml]   [2009]   [Apr]   [10]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
SubjectRe: [PATCH 1/1] cciss: resubmit export uid, model, vendor, rev to sysfs
From
Date
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



\
 
 \ /
  Last update: 2009-04-10 19:23    [W:0.055 / U:0.464 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site