On Thu, Sep 25, 2008 at 08:15:05AM -0600, Brad Nicholes wrote: > <[EMAIL PROTECTED]> wrote: > > if (strcasecmp(cb->name, metric_info[i].name) == 0) > > { > > - sprintf (modular_desc, "%s (module %s)", > > metric_info[i].desc, cb->modp->module_name); > > + snprintf (modular_desc, sizeof(modular_desc), > > + "%s (module %s)", > > + metric_info[i].desc, > > + cb->modp->module_name); > > + > > desc = (char*)modular_desc; > > break; > > } > > When copying into the buffer, shouldn't the length be sizeof(modular_desc)-1 > rather than the full length of the buffer?
the length is the maximum allowed number of bytes that will be written in the buffer so sizeof(modular_desc) is a more natural fit since otherwise the buffer will be artificially restricted by 1 byte. > It needs to allow for a NULL terminator. snprintf is defined with C99 and I don't have an specification handy but the man page for it in Linux says : "The functions snprintf() and vsnprintf() write at most size bytes (including the trailing null byte ('\0')) to str." so the NULL terminator should be included in the length requested, and a quick test with gcc 4.1.2 and the following code shows that it is indeed terminating the buffer and truncating the result as needed to do so. #include <string.h> #include <stdio.h> #define BUFFSIZE 4 int main(int argc, char *argv[]) { char buffer[BUFFSIZE]; char *source = "test"; int n; for (n =0 ; n < BUFFSIZE; n++) buffer[n] = 'A'; printf("%s\n", buffer); snprintf(buffer, sizeof(buffer), "%s", source); printf("%s\n", buffer); printf("%d\n", buffer[BUFFSIZE]); return (0); } to avoid truncation modular_desc should be large enough but now is 1024 bytes long and most likely big enough already (if probably too big) Carlo ------------------------------------------------------------------------- This SF.Net email is sponsored by the Moblin Your Move Developer's challenge Build the coolest Linux based applications with Moblin SDK & win great prizes Grand prize is a trip for two to an Open Source event anywhere in the world http://moblin-contest.org/redirect.php?banner_id=100&url=/ _______________________________________________ Ganglia-developers mailing list Ganglia-developers@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/ganglia-developers