* [PATCH] Use strlen instead of sizeof on char* string
@ 2012-11-13 15:47 Alexander Stein
2012-11-13 20:26 ` Marc Kleine-Budde
0 siblings, 1 reply; 6+ messages in thread
From: Alexander Stein @ 2012-11-13 15:47 UTC (permalink / raw)
To: linux-can; +Cc: Alexander Stein
This bug was detected by the clang warning:
libsocketcan.c:384:16: warning: argument to 'sizeof' in 'strncmp' call
is the same expression as the source; did you mean to provide an
explicit length? [-Wsizeof-pointer-memaccess]
sizeof(name)) != 0)
~~~~~~~^~~~~~
Signed-off-by: Alexander Stein <alexander.stein@systec-electronic.com>
---
src/libsocketcan.c | 3 ++-
1 files changed, 2 insertions(+), 1 deletions(-)
diff --git a/src/libsocketcan.c b/src/libsocketcan.c
index fedcbdc..3ad2a6c 100644
--- a/src/libsocketcan.c
+++ b/src/libsocketcan.c
@@ -364,6 +364,7 @@ static int do_get_nl_link(int fd, __u8 acquire, const char *name, void *res)
nl_msg = NLMSG_NEXT(nl_msg, u_msglen)) {
int type = nl_msg->nlmsg_type;
int len;
+ int namelen = strlen(name);
if (type == NLMSG_DONE) {
done++;
@@ -381,7 +382,7 @@ static int do_get_nl_link(int fd, __u8 acquire, const char *name, void *res)
if (strncmp
((char *)RTA_DATA(tb[IFLA_IFNAME]), name,
- sizeof(name)) != 0)
+ namelen) != 0)
continue;
if (tb[IFLA_LINKINFO])
--
1.7.8.6
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH] Use strlen instead of sizeof on char* string
2012-11-13 15:47 [PATCH] Use strlen instead of sizeof on char* string Alexander Stein
@ 2012-11-13 20:26 ` Marc Kleine-Budde
2012-11-14 8:55 ` [PATCH] Use strcmp instead of strncmp Kurt Van Dijck
0 siblings, 1 reply; 6+ messages in thread
From: Marc Kleine-Budde @ 2012-11-13 20:26 UTC (permalink / raw)
To: Alexander Stein; +Cc: linux-can
[-- Attachment #1: Type: text/plain, Size: 1680 bytes --]
On 11/13/2012 04:47 PM, Alexander Stein wrote:
> This bug was detected by the clang warning:
> libsocketcan.c:384:16: warning: argument to 'sizeof' in 'strncmp' call
> is the same expression as the source; did you mean to provide an
> explicit length? [-Wsizeof-pointer-memaccess]
> sizeof(name)) != 0)
> ~~~~~~~^~~~~~
>
> Signed-off-by: Alexander Stein <alexander.stein@systec-electronic.com>
> ---
> src/libsocketcan.c | 3 ++-
> 1 files changed, 2 insertions(+), 1 deletions(-)
>
> diff --git a/src/libsocketcan.c b/src/libsocketcan.c
> index fedcbdc..3ad2a6c 100644
> --- a/src/libsocketcan.c
> +++ b/src/libsocketcan.c
> @@ -364,6 +364,7 @@ static int do_get_nl_link(int fd, __u8 acquire, const char *name, void *res)
> nl_msg = NLMSG_NEXT(nl_msg, u_msglen)) {
> int type = nl_msg->nlmsg_type;
> int len;
> + int namelen = strlen(name);
>
> if (type == NLMSG_DONE) {
> done++;
> @@ -381,7 +382,7 @@ static int do_get_nl_link(int fd, __u8 acquire, const char *name, void *res)
>
> if (strncmp
> ((char *)RTA_DATA(tb[IFLA_IFNAME]), name,
> - sizeof(name)) != 0)
> + namelen) != 0)
What about using strcmp() instead. AFAIK it will compare until it finds
the end of the string in one of the strings, so it should be no
difference here?
> continue;
>
> if (tb[IFLA_LINKINFO])
>
Marc
--
Pengutronix e.K. | Marc Kleine-Budde |
Industrial Linux Solutions | Phone: +49-231-2826-924 |
Vertretung West/Dortmund | Fax: +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686 | http://www.pengutronix.de |
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 259 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] Use strcmp instead of strncmp
2012-11-13 20:26 ` Marc Kleine-Budde
@ 2012-11-14 8:55 ` Kurt Van Dijck
2012-11-14 9:07 ` Alexander Stein
0 siblings, 1 reply; 6+ messages in thread
From: Kurt Van Dijck @ 2012-11-14 8:55 UTC (permalink / raw)
To: Marc Kleine-Budde; +Cc: Alexander Stein, linux-can
On Tue, Nov 13, 2012 at 09:26:50PM +0100, Marc Kleine-Budde wrote:
> On 11/13/2012 04:47 PM, Alexander Stein wrote:
> > This bug was detected by the clang warning:
> > libsocketcan.c:384:16: warning: argument to 'sizeof' in 'strncmp' call
> > is the same expression as the source; did you mean to provide an
> > explicit length? [-Wsizeof-pointer-memaccess]
> > sizeof(name)) != 0)
> > ~~~~~~~^~~~~~
strlen() requires a null terminator. Is that always present?
Unfortunately, I've no access to the libsocketcan code today...
> >
> > Signed-off-by: Alexander Stein <alexander.stein@systec-electronic.com>
> > ---
> > src/libsocketcan.c | 3 ++-
> > 1 files changed, 2 insertions(+), 1 deletions(-)
> >
> > diff --git a/src/libsocketcan.c b/src/libsocketcan.c
> > index fedcbdc..3ad2a6c 100644
> > --- a/src/libsocketcan.c
> > +++ b/src/libsocketcan.c
> > @@ -364,6 +364,7 @@ static int do_get_nl_link(int fd, __u8 acquire, const char *name, void *res)
> > nl_msg = NLMSG_NEXT(nl_msg, u_msglen)) {
> > int type = nl_msg->nlmsg_type;
> > int len;
> > + int namelen = strlen(name);
> >
> > if (type == NLMSG_DONE) {
> > done++;
> > @@ -381,7 +382,7 @@ static int do_get_nl_link(int fd, __u8 acquire, const char *name, void *res)
> >
> > if (strncmp
> > ((char *)RTA_DATA(tb[IFLA_IFNAME]), name,
> > - sizeof(name)) != 0)
> > + namelen) != 0)
>
> What about using strcmp() instead. AFAIK it will compare until it finds
> the end of the string in one of the strings, so it should be no
> difference here?
if name contains a null terminator, using strcmp() is even better.
>
> > continue;
> >
> > if (tb[IFLA_LINKINFO])
> >
>
> Marc
>
> --
> Pengutronix e.K. | Marc Kleine-Budde |
> Industrial Linux Solutions | Phone: +49-231-2826-924 |
> Vertretung West/Dortmund | Fax: +49-5121-206917-5555 |
> Amtsgericht Hildesheim, HRA 2686 | http://www.pengutronix.de |
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] Use strcmp instead of strncmp
2012-11-14 8:55 ` [PATCH] Use strcmp instead of strncmp Kurt Van Dijck
@ 2012-11-14 9:07 ` Alexander Stein
2012-11-14 10:04 ` Kurt Van Dijck
0 siblings, 1 reply; 6+ messages in thread
From: Alexander Stein @ 2012-11-14 9:07 UTC (permalink / raw)
To: Kurt Van Dijck; +Cc: Marc Kleine-Budde, linux-can
Hello,
On Wednesday 14 November 2012 09:55:37, Kurt Van Dijck wrote:
> On Tue, Nov 13, 2012 at 09:26:50PM +0100, Marc Kleine-Budde wrote:
> > On 11/13/2012 04:47 PM, Alexander Stein wrote:
> > > This bug was detected by the clang warning:
> > > libsocketcan.c:384:16: warning: argument to 'sizeof' in 'strncmp' call
> > > is the same expression as the source; did you mean to provide an
> > > explicit length? [-Wsizeof-pointer-memaccess]
> > > sizeof(name)) != 0)
> > > ~~~~~~~^~~~~~
> strlen() requires a null terminator. Is that always present?
> Unfortunately, I've no access to the libsocketcan code today...
> > >
> > > Signed-off-by: Alexander Stein <alexander.stein@systec-electronic.com>
> > > ---
> > > src/libsocketcan.c | 3 ++-
> > > 1 files changed, 2 insertions(+), 1 deletions(-)
> > >
> > > diff --git a/src/libsocketcan.c b/src/libsocketcan.c
> > > index fedcbdc..3ad2a6c 100644
> > > --- a/src/libsocketcan.c
> > > +++ b/src/libsocketcan.c
> > > @@ -364,6 +364,7 @@ static int do_get_nl_link(int fd, __u8 acquire,
const char *name, void *res)
> > > nl_msg = NLMSG_NEXT(nl_msg, u_msglen)) {
> > > int type = nl_msg->nlmsg_type;
> > > int len;
> > > + int namelen = strlen(name);
> > >
> > > if (type == NLMSG_DONE) {
> > > done++;
> > > @@ -381,7 +382,7 @@ static int do_get_nl_link(int fd, __u8 acquire,
const char *name, void *res)
> > >
> > > if (strncmp
> > > ((char *)RTA_DATA(tb[IFLA_IFNAME]), name,
> > > - sizeof(name)) != 0)
> > > + namelen) != 0)
> >
> > What about using strcmp() instead. AFAIK it will compare until it finds
> > the end of the string in one of the strings, so it should be no
> > difference here?
> if name contains a null terminator, using strcmp() is even better.
name is passed directly from public API functions like can_get_state. So the
user has to provide the null terminator. As this cannot be garuanteed it might
better to use strlen on (char *)RTA_DATA(tb[IFLA_IFNAME]).
BTW: there is no need to introduce a new variable. sizeof can simply be
replaced by strlen.
Best regards,
Alexander
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] Use strcmp instead of strncmp
2012-11-14 9:07 ` Alexander Stein
@ 2012-11-14 10:04 ` Kurt Van Dijck
2012-11-14 10:10 ` Marc Kleine-Budde
0 siblings, 1 reply; 6+ messages in thread
From: Kurt Van Dijck @ 2012-11-14 10:04 UTC (permalink / raw)
To: Alexander Stein; +Cc: Marc Kleine-Budde, linux-can
On Wed, Nov 14, 2012 at 10:07:15AM +0100, Alexander Stein wrote:
> Hello,
>
> On Wednesday 14 November 2012 09:55:37, Kurt Van Dijck wrote:
> > On Tue, Nov 13, 2012 at 09:26:50PM +0100, Marc Kleine-Budde wrote:
> > > On 11/13/2012 04:47 PM, Alexander Stein wrote:
> > > > This bug was detected by the clang warning:
> > > > libsocketcan.c:384:16: warning: argument to 'sizeof' in 'strncmp' call
> > > > is the same expression as the source; did you mean to provide an
> > > > explicit length? [-Wsizeof-pointer-memaccess]
> > > > sizeof(name)) != 0)
> > > > ~~~~~~~^~~~~~
> > strlen() requires a null terminator. Is that always present?
> > Unfortunately, I've no access to the libsocketcan code today...
> > > >
> > > > Signed-off-by: Alexander Stein <alexander.stein@systec-electronic.com>
> > > > ---
> > > > src/libsocketcan.c | 3 ++-
> > > > 1 files changed, 2 insertions(+), 1 deletions(-)
> > > >
> > > > diff --git a/src/libsocketcan.c b/src/libsocketcan.c
> > > > index fedcbdc..3ad2a6c 100644
> > > > --- a/src/libsocketcan.c
> > > > +++ b/src/libsocketcan.c
> > > > @@ -364,6 +364,7 @@ static int do_get_nl_link(int fd, __u8 acquire,
> const char *name, void *res)
> > > > nl_msg = NLMSG_NEXT(nl_msg, u_msglen)) {
> > > > int type = nl_msg->nlmsg_type;
> > > > int len;
> > > > + int namelen = strlen(name);
> > > >
> > > > if (type == NLMSG_DONE) {
> > > > done++;
> > > > @@ -381,7 +382,7 @@ static int do_get_nl_link(int fd, __u8 acquire,
> const char *name, void *res)
> > > >
> > > > if (strncmp
> > > > ((char *)RTA_DATA(tb[IFLA_IFNAME]), name,
> > > > - sizeof(name)) != 0)
> > > > + namelen) != 0)
> > >
> > > What about using strcmp() instead. AFAIK it will compare until it finds
> > > the end of the string in one of the strings, so it should be no
> > > difference here?
> > if name contains a null terminator, using strcmp() is even better.
>
> name is passed directly from public API functions like can_get_state. So the
> user has to provide the null terminator. As this cannot be garuanteed
I think we should assume that the user passes a null terminated string.
Otherwise, we must question if the user passes a valid address. I don't
like to go that way.
strcmp() will just work.
Kind regards,
Kurt
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] Use strcmp instead of strncmp
2012-11-14 10:04 ` Kurt Van Dijck
@ 2012-11-14 10:10 ` Marc Kleine-Budde
0 siblings, 0 replies; 6+ messages in thread
From: Marc Kleine-Budde @ 2012-11-14 10:10 UTC (permalink / raw)
To: Alexander Stein, linux-can
[-- Attachment #1: Type: text/plain, Size: 2621 bytes --]
On 11/14/2012 11:04 AM, Kurt Van Dijck wrote:
> On Wed, Nov 14, 2012 at 10:07:15AM +0100, Alexander Stein wrote:
>> Hello,
>>
>> On Wednesday 14 November 2012 09:55:37, Kurt Van Dijck wrote:
>>> On Tue, Nov 13, 2012 at 09:26:50PM +0100, Marc Kleine-Budde wrote:
>>>> On 11/13/2012 04:47 PM, Alexander Stein wrote:
>>>>> This bug was detected by the clang warning:
>>>>> libsocketcan.c:384:16: warning: argument to 'sizeof' in 'strncmp' call
>>>>> is the same expression as the source; did you mean to provide an
>>>>> explicit length? [-Wsizeof-pointer-memaccess]
>>>>> sizeof(name)) != 0)
>>>>> ~~~~~~~^~~~~~
>>> strlen() requires a null terminator. Is that always present?
>>> Unfortunately, I've no access to the libsocketcan code today...
>>>>>
>>>>> Signed-off-by: Alexander Stein <alexander.stein@systec-electronic.com>
>>>>> ---
>>>>> src/libsocketcan.c | 3 ++-
>>>>> 1 files changed, 2 insertions(+), 1 deletions(-)
>>>>>
>>>>> diff --git a/src/libsocketcan.c b/src/libsocketcan.c
>>>>> index fedcbdc..3ad2a6c 100644
>>>>> --- a/src/libsocketcan.c
>>>>> +++ b/src/libsocketcan.c
>>>>> @@ -364,6 +364,7 @@ static int do_get_nl_link(int fd, __u8 acquire,
>> const char *name, void *res)
>>>>> nl_msg = NLMSG_NEXT(nl_msg, u_msglen)) {
>>>>> int type = nl_msg->nlmsg_type;
>>>>> int len;
>>>>> + int namelen = strlen(name);
>>>>>
>>>>> if (type == NLMSG_DONE) {
>>>>> done++;
>>>>> @@ -381,7 +382,7 @@ static int do_get_nl_link(int fd, __u8 acquire,
>> const char *name, void *res)
>>>>>
>>>>> if (strncmp
>>>>> ((char *)RTA_DATA(tb[IFLA_IFNAME]), name,
>>>>> - sizeof(name)) != 0)
>>>>> + namelen) != 0)
>>>>
>>>> What about using strcmp() instead. AFAIK it will compare until it finds
>>>> the end of the string in one of the strings, so it should be no
>>>> difference here?
>>> if name contains a null terminator, using strcmp() is even better.
>>
>> name is passed directly from public API functions like can_get_state. So the
>> user has to provide the null terminator. As this cannot be garuanteed
> I think we should assume that the user passes a null terminated string.
> Otherwise, we must question if the user passes a valid address. I don't
> like to go that way.
>
> strcmp() will just work.
+1
Marc
--
Pengutronix e.K. | Marc Kleine-Budde |
Industrial Linux Solutions | Phone: +49-231-2826-924 |
Vertretung West/Dortmund | Fax: +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686 | http://www.pengutronix.de |
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 259 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2012-11-14 10:10 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-11-13 15:47 [PATCH] Use strlen instead of sizeof on char* string Alexander Stein
2012-11-13 20:26 ` Marc Kleine-Budde
2012-11-14 8:55 ` [PATCH] Use strcmp instead of strncmp Kurt Van Dijck
2012-11-14 9:07 ` Alexander Stein
2012-11-14 10:04 ` Kurt Van Dijck
2012-11-14 10:10 ` Marc Kleine-Budde
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).