Documentation: Fix NUL termination of strncpy
Subsystems (3)
PATCH 1/1 Documentation: Fix NUL termination of strncpy
Ensure the copied strings are NUL terminated.
--- Documentation/accounting/getdelays.c | 2 +- Documentation/hwmon/hpfall.c | 2 +- Documentation/networking/radiotap-headers.txt | 2 +- .../networking/timestamping/timestamping.c | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-)
Rationale (please correct me if I'm wrong):
For a larger source string, strncpy only NUL terminates when the size argument is _less_ than sizeof(destination string). strlcpy does this when it equals sizeof (dest), but does not overwrite NULs in the higher chars.
Because cpumask is global I used strncpy, for the extra NULs. devname is local so I think strlcpy can be used. For {device,hwtstamp}.ifr_name I thought the strlcpy could be used since ifr_name is zeroed upon creation of device and hwtstamp.
Oh, and the parenthesis in radiotap-headers.txt should not be there.
Roel
Documentation/accounting/getdelays.c +1 -1
@@ -303,7 +303,7 @@ int main(int argc, char *argv[])err (1 ,"Invalid rcv buf size\n" );break ;case 'm' : -strncpy (cpumask, optarg,sizeof (cpumask)); +strncpy (cpumask, optarg,sizeof (cpumask) -1 ); maskset =1 ;printf ("cpumask %s maskset %d\n" , cpumask, maskset);break ;
Documentation/hwmon/hpfall.c +1 -1
@@ -27,7 +27,7 @@ int set_unload_heads_path(char *device)if (strlen (device) <=5 ||strncmp (device,"/dev/" ,5 ) !=0 )return -EINVAL ; -strncpy (devname, device +5 ,sizeof (devname)); +strlcpy (devname, device +5 ,sizeof (devname));snprintf (unload_heads_path,sizeof (unload_heads_path),"/sys/block/%s/device/unload_heads" , devname);
Documentation/networking/radiotap-headers.txt +1 -1
@@ -126,7 +126,7 @@ int MyFunction(u8 * buf, int buflen) case IEEE80211_RADIOTAP_ANTENNA: /* radiotap uses 0 for 1st ant */ - antenna = *iterator.this_arg); + antenna = *iterator.this_arg; break; case IEEE80211_RADIOTAP_DBM_TX_POWER:
Documentation/networking/timestamping/timestamping.c +2 -2
@@ -374,12 +374,12 @@ int main(int argc, char **argv)bail ("socket" );memset (&device,0 ,sizeof (device)); -strncpy (device.ifr_name, interface,sizeof (device.ifr_name)); +strlcpy (device.ifr_name, interface,sizeof (device.ifr_name));if (ioctl (sock,SIOCGIFADDR , &device) <0 )bail ("getting interface IP address" );memset (&hwtstamp,0 ,sizeof (hwtstamp)); -strncpy (hwtstamp.ifr_name, interface,sizeof (hwtstamp.ifr_name)); +strlcpy (hwtstamp.ifr_name, interface,sizeof (hwtstamp.ifr_name)); hwtstamp.ifr_data = (void *)&hwconfig;memset (&hwconfig,0 ,sizeof (hwconfig)); hwconfig.tx_type =
Ensure the copied strings are NUL terminated.
---
Quoted 7 lines
Rationale (please correct me if I'm wrong):
For a larger source string, strncpy only NUL terminates when the size argument is _less_ than sizeof(destination string). strlcpy does this when it equals sizeof (dest), but does not overwrite NULs in the higher chars.
Quoted 8 lines
Because cpumask is global I used strncpy, for the extra NULs. devname is local so I think strlcpy can be used. For {device,hwtstamp}.ifr_name I thought the strlcpy could be used since
ifr_nameis zeroed upon creation of device and hwtstamp.Oh, and the parenthesis in radiotap-headers.txt should not be there.
See http://markmail.org/message/5ckmbipstgslzolf
(To David Wagner: this was what you meant.)
I forgot strlcpy is not present in old glibc versions, please use this one instead.
Roel
Documentation/accounting/getdelays.c | 2 +- Documentation/hwmon/hpfall.c | 2 +- Documentation/networking/radiotap-headers.txt | 2 +- .../networking/timestamping/timestamping.c | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-)
Documentation/accounting/getdelays.c +1 -1
@@ -303,7 +303,7 @@ int main(int argc, char *argv[])err (1 ,"Invalid rcv buf size\n" );break ;case 'm' : -strncpy (cpumask, optarg,sizeof (cpumask)); +strncpy (cpumask, optarg,sizeof (cpumask) -1 ); maskset =1 ;printf ("cpumask %s maskset %d\n" , cpumask, maskset);break ;
Documentation/hwmon/hpfall.c +1 -1
@@ -27,7 +27,7 @@ int set_unload_heads_path(char *device)if (strlen (device) <=5 ||strncmp (device,"/dev/" ,5 ) !=0 )return -EINVAL ; -strncpy (devname, device +5 ,sizeof (devname)); +strncpy (devname, device +5 ,sizeof (devname) -1 );snprintf (unload_heads_path,sizeof (unload_heads_path),"/sys/block/%s/device/unload_heads" , devname);
Documentation/networking/radiotap-headers.txt +1 -1
@@ -126,7 +126,7 @@ int MyFunction(u8 * buf, int buflen) case IEEE80211_RADIOTAP_ANTENNA: /* radiotap uses 0 for 1st ant */ - antenna = *iterator.this_arg); + antenna = *iterator.this_arg; break; case IEEE80211_RADIOTAP_DBM_TX_POWER:
Documentation/networking/timestamping/timestamping.c +2 -2
@@ -374,12 +374,12 @@ int main(int argc, char **argv)bail ("socket" );memset (&device,0 ,sizeof (device)); -strncpy (device.ifr_name, interface,sizeof (device.ifr_name)); +strncpy (device.ifr_name, interface,sizeof (device.ifr_name) -1 );if (ioctl (sock,SIOCGIFADDR , &device) <0 )bail ("getting interface IP address" );memset (&hwtstamp,0 ,sizeof (hwtstamp)); -strncpy (hwtstamp.ifr_name, interface,sizeof (hwtstamp.ifr_name)); +strncpy (hwtstamp.ifr_name, interface,sizeof (hwtstamp.ifr_name) -1 ); hwtstamp.ifr_data = (void *)&hwconfig;memset (&hwconfig,0 ,sizeof (hwconfig)); hwconfig.tx_type =
Ensure the copied strings are NUL terminated.
---
Quoted 1 line
Quoted 1 line
Rationale (please correct me if I'm wrong):
I was confused. strncpy requires explicit NUL termination or it depends on a NUL in the source string.
Documentation/accounting/getdelays.c | 3 ++- Documentation/hwmon/hpfall.c | 3 ++- Documentation/networking/radiotap-headers.txt | 2 +- .../networking/timestamping/timestamping.c | 6 ++++-- 4 files changed, 9 insertions(+), 5 deletions(-)
Documentation/accounting/getdelays.c +2 -1
@@ -303,7 +303,8 @@ int main(int argc, char *argv[])err (1 ,"Invalid rcv buf size\n" );break ;case 'm' : -strncpy (cpumask, optarg,sizeof (cpumask)); +strncpy (cpumask, optarg,sizeof (cpumask) -1 ); + cpumask[sizeof (cpumask) -1 ] ='\0' ; maskset =1 ;printf ("cpumask %s maskset %d\n" , cpumask, maskset);break ;
Documentation/hwmon/hpfall.c +2 -1
@@ -27,7 +27,8 @@ int set_unload_heads_path(char *device)if (strlen (device) <=5 ||strncmp (device,"/dev/" ,5 ) !=0 )return -EINVAL ; -strncpy (devname, device +5 ,sizeof (devname)); +strncpy (devname, device +5 ,sizeof (devname) -1 ); + devname[sizeof (devname) -1 ] ='\0' ;snprintf (unload_heads_path,sizeof (unload_heads_path),"/sys/block/%s/device/unload_heads" , devname);
Documentation/networking/radiotap-headers.txt +1 -1
@@ -126,7 +126,7 @@ int MyFunction(u8 * buf, int buflen) case IEEE80211_RADIOTAP_ANTENNA: /* radiotap uses 0 for 1st ant */ - antenna = *iterator.this_arg); + antenna = *iterator.this_arg; break; case IEEE80211_RADIOTAP_DBM_TX_POWER:
Documentation/networking/timestamping/timestamping.c +4 -2
@@ -374,12 +374,14 @@ int main(int argc, char **argv)bail ("socket" );memset (&device,0 ,sizeof (device)); -strncpy (device.ifr_name, interface,sizeof (device.ifr_name)); +strncpy (device.ifr_name, interface,sizeof (device.ifr_name) -1 ); + device.ifr_name[sizeof (device.ifr_name) -1 ] ='\0' ;if (ioctl (sock,SIOCGIFADDR , &device) <0 )bail ("getting interface IP address" );memset (&hwtstamp,0 ,sizeof (hwtstamp)); -strncpy (hwtstamp.ifr_name, interface,sizeof (hwtstamp.ifr_name)); +strncpy (hwtstamp.ifr_name, interface,sizeof (hwtstamp.ifr_name) -1 ); + hwtstamp.ifr_name[sizeof (hwtstamp.ifr_name) -1 ] ='\0' ; hwtstamp.ifr_data = (void *)&hwconfig;memset (&hwconfig,0 ,sizeof (hwconfig)); hwconfig.tx_type =
Quoted 11 lines · Roel Kluin
Ensure the copied strings are NUL terminated.
Signed-off-by Roel Kluin <[email protected]>---
Quoted 1 line
Rationale (please correct me if I'm wrong):
Documentation/accounting/getdelays.c +0
-0
Documentation/accounting/getdelays.c int main(int argc, char *argv[]) · line 306
Quoted 5 lines
Documentation/accounting/getdelays.c +1
-1@@ -303,7 +303,7 @@ int main(int argc, char *argv[])err (1 ,"Invalid rcv buf size\n" );break ;case 'm' : -strncpy (cpumask, optarg,sizeof (cpumask)); +strncpy (cpumask, optarg,sizeof (cpumask) -1 );
You need to explicitly NUL terminate strncpy():
http://www.pixelbeat.org/programming/gcc/string_buffers.html
cheers,
Pádraig.
Quoted 2 lines · Pádraig Brady
You need to explicitly NUL terminate
strncpy():
http://www.pixelbeat.org/programming/gcc/string_buffers.html
Umm. Actually, no. It's *nice* if you explicitly do it. However, it's not strictly *required*. Consider this code:
struct a {
int b, c;
char d[20];
}bzero(a,sizeof(a)); /* voila! a->d[19] is now a \0 */
Perfectly valid and we do it all the time. Your referenced web page comments that using memset() or bzero() is inefficient - I wonder if they actually *tested* the difference between using one memset() or bzero() on a large structure compared to lots and lots of explicit initialization statements (i-cache issues probably most important there). The downside is that there's a greater chance of some programmer refactoring code and bollixing it up. (The astute reader will remember a short thread about this not too long ago.. :)
And if you're truly careful and always use the strn- variants of the string functions, it's actually possible to *not* null-terminate the strings. But it results in very brittle code and makes Baby Andrew cry, mostly because when somebody adds a printf() to debug the brittle code, it suddenly becomes even more brittle... :)