Suggestion on "int len" sanity
Quoted 4 lines · Willy Tarreau
Quoted 1 line
On the other hand, when a variable is named "len" or "length", it is usually used for length and never should go negative. So could I suggest that the declarations of these variables to be uniformized to "
size_t", via a gradual but sysmatic cleanup?Probably true for most cases, but be careful of code which would use -1 to report some errors if such thing exists.
I agree that they are probably not all replaceable, and care must be taken.
Examples:
1. In the types of sys_[gs]ethostname, sys_[gs]etdomainname, "int len" could be replaced by "unsigned int" or "size_t" and sanity check simplified.
2. In mm/shmem.c, shmem_symlink(), we have "len = strlen(symname) + 1;". Although it is highly improbable that strlen(symname) overflows, it is more correct to declare "size_t len;".
3. The similar situation occurs in fs/namei.c, vfs_readlink(). Here it does not matter if len is declared to be unsigned, but for size_t, we have to take care about the size of size_t.
Signature · 2 lines
XIAO Gang (~{P$8U~}) [email protected]
home page: pcmath126.unice.fr/xiao.html Quoted 6 lines · XIAO Gang
Examples:
1. In the types of sys_[gs]ethostname, sys_[gs]etdomainname, "int len" could be replaced
by "unsigned int" or "size_t" and sanity check simplified.
If you really want that fun, try changing it to "unsigned long long" on your private machine and do some testing.
Hint: arch/i386/kernel/syscall_table.S
Quoted 4 lines
2. In
mm/shmem.c,shmem_symlink(), we have "len = strlen(symname) + 1;". Although it is highly
improbable that strlen(symname) overflows, it is more correct to declare "size_tlen;".
Yep, looks sane.
Quoted 4 lines
3. The similar situation occurs in
fs/namei.c,vfs_readlink(). Here it does not matter if len
is declared to be unsigned, but forsize_t, we have to take care about the size ofsize_t.
You could possibly change the code to:
int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
{
union {
unsigned len;
int ret;
} u;
u.ret = PTR_ERR(link);
if (IS_ERR(link))
goto out;
u.len = strlen(link);
if (u.len > (unsigned) buflen)
u.len = buflen;
if (copy_to_user(buffer, link, u.len))
u.ret = -EFAULT;
out:
return u.ret;
}But what would we gain, except for a few additional lines?
Jörn
Signature · 2 lines
Happiness isn't having what you want, it's wanting what you have. -- unknown
Quoted 10 lines · Jörn Engel
Quoted 6 lines · XIAO Gang
Examples:
1. In the types of sys_[gs]ethostname, sys_[gs]etdomainname, "int len" could be replaced by "unsigned int" or "
size_t" and sanity check simplified.
Quoted 5 lines
If you really want that fun, try changing it to "unsigned long long" on your private machine and do some testing.
Hint:
arch/i386/kernel/syscall_table.S
I know; I might try to do something later. The question here is to find the best balancing point between what
are better replaced and what are not. I would hesitate a lot before doing things as below which are more likely to introduce new bugs than to avoid old ones.
Quoted 33 lines
Quoted 4 lines
3. The similar situation occurs in
fs/namei.c,vfs_readlink(). Here it does not matter if len is declared to be unsigned, but forsize_t, we have to take care about the size ofsize_t.You could possibly change the code to:
int
vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link){ union { unsigned len; int ret; } u; u.ret = PTR_ERR(link); if (IS_ERR(link)) goto out; u.len = strlen(link); if (u.len > (unsigned) buflen) u.len = buflen; if (copy_to_user(buffer, link, u.len)) u.ret = -EFAULT; out: return u.ret; }But what would we gain, except for a few additional lines?
Jörn
Signature · 2 lines
XIAO Gang (~{P$8U~}) [email protected]
home page: pcmath126.unice.fr/xiao.html Quoted 6 lines · XIAO Gang
Quoted 1 line
I know; I might try to do something later. The question here is to find the best balancing point between what
are better replaced and what are not. I would hesitate a lot before doing things as below which are more likely to introduce new bugs than to avoid old ones.
In some cases a comment like "this sucks, but we have to do it because.." helps. Example 2 can also be changed to size_t, that one makes sense.
"grep 'int len'" really is a decent code checker, I agree. The hard part is finding the 2% real problems among the output. And possibly other cases where s/int/size_t/ doesn't fix anything, but makes slightly more sense.
Jörn
Signature · 3 lines
Man darf nicht das, was uns unwahrscheinlich und unnatürlich erscheint, mit dem verwechseln, was absolut unmöglich ist. -- Carl Friedrich Gauß
Quoted 6 lines · On Thu, 2 Jun 2005, [iso-8859-1] J�rn Engel wrote:
Quoted 4 lines · XIAO Gang
3. The similar situation occurs in
fs/namei.c,vfs_readlink(). Here it does not matter if len
is declared to be unsigned, but forsize_t, we have to take care about the size ofsize_t.
Quoted 6 lines
You could possibly change the code to:
int
vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link){ union { unsigned len;
^^^^^^^^
Plain unsigned is deprecated.
Quoted 2 lines
int ret; } u;
Ugh...
Quoted 13 lines
u.ret = PTR_ERR(link); if (IS_ERR(link)) goto out; u.len = strlen(link); if (u.len > (unsigned) buflen) u.len = buflen; if (copy_to_user(buffer, link, u.len)) u.ret = -EFAULT; out: return u.ret; }
buflen should be size_t.
Since the return value may be negative, it should be signed. But int is not an option, since size_t is 64 bit on 64-bit machines, while int is still 32-bit. So the return type should be ssize_t.
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]
In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds
Quoted 5 lines · Geert Uytterhoeven
Quoted 4 lines · On Thu, 2 Jun 2005, [iso-8859-1] J?rn Engel wrote:
int
vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link){ union { unsigned len;
Quoted 7 lines
^^^^^^^^Plain unsigned is deprecated.
Quoted 2 lines
int ret; } u;Ugh...
Ugh, indeed - who the hell had come up with _that_?
Quoted 14 lines
Quoted 13 lines
u.ret = PTR_ERR(link); if (IS_ERR(link)) goto out; u.len = strlen(link); if (u.len > (unsigned) buflen) u.len = buflen; if (copy_to_user(buffer, link, u.len)) u.ret = -EFAULT; out: return u.ret; }
Quoted 5 lines
buflen should be
size_t.Since the return value may be negative, it should be signed. But int is not an option, since
size_tis 64 bit on 64-bit machines, while int is still 32-bit. So the return type should bessize_t.
BS from the beginning to end - we are talking about symlink bodies. These are *not* going to be 2Gb long.
And that quite neatly shows why the whole plan is a non-starter - you need to understand what's going on in the code being mangled.
Quoted 2 lines · Al Viro
Ugh, indeed - who the hell had come up with _that_?
/me did.
Should I add explicit <joke> tags next time? Thought it would be obvious, esp. to you.
Jörn
Signature · 2 lines
I don't understand it. Nobody does. -- Richard P. Feynman
Quoted 4 lines · Geert Uytterhoeven
Quoted 2 lines
union { unsigned len;^^^^^^^^Plain unsigned is deprecated.
Says who?
Andreas.
Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected] SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5 "And now for something completely different."
Quoted 8 lines · Andreas Schwab
Quoted 4 lines · Geert Uytterhoeven
Quoted 2 lines
union { unsigned len;^^^^^^^^Plain unsigned is deprecated.
Says who?
Sorry, forgot to add the
`Signed-Off-by: Geert Uytterhoeven <[email protected]>' line :-)
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]
In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds
Quoted 5 lines · Geert Uytterhoeven
Quoted 4 lines · Andreas Schwab
Quoted 2 lines · Geert Uytterhoeven
Quoted 2 lines
union { unsigned len;
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
^^^^^^^^Plain unsigned is deprecated.
Says who?
Sorry, forgot to add the
`Signed-Off-by: Geert Uytterhoeven <[email protected]>' line :-)
Who deprecated it?
Andreas.
Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected] SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5 "And now for something completely different."
On Fri, 03 Jun 2005 16:38:07 +0200 Andreas Schwab wrote:
| Geert Uytterhoeven <[email protected]> writes: | | > On Fri, 3 Jun 2005, Andreas Schwab wrote: | >> Geert Uytterhoeven <[email protected]> writes: | >> | >> >> union { | >> >> unsigned len; | >> > ^^^^^^^^ | >> > Plain unsigned is deprecated. | >> | >> Says who? | > | > Sorry, forgot to add the | > `Signed-Off-by: Geert Uytterhoeven <[email protected]>' line :-) | | Who deprecated it?
Not technically deprecated, just undesirable in the kernel sources.
---
~Randy
Quoted 6 lines · randy_dunlap
| > Sorry, forgot to add the | > `Signed-Off-by: Geert Uytterhoeven <[email protected]>' line :-) | | Who deprecated it?Not technically deprecated, just undesirable in the kernel sources.
Don't tell the device mapper folks. Uhm. And why is it depreciated?
Signature · 4 lines
High Availability & Clustering SUSE Labs, Research and Development SUSE LINUX Products GmbH - A Novell Business -- Charles Darwin "Ignorance more frequently begets confidence than does knowledge"
On Fri, 3 Jun 2005 20:09:41 +0200 Lars Marowsky-Bree wrote:
| On 2005-06-03T11:04:36, randy_dunlap <[email protected]> wrote: | | > | > Sorry, forgot to add the | > | > `Signed-Off-by: Geert Uytterhoeven <[email protected]>' line :-) | > | | > | Who deprecated it? | > | > Not technically deprecated, just undesirable in the kernel sources. | | Don't tell the device mapper folks. Uhm. And why is it depreciated?
what's depreciated?
Kernel source prefers more explicit typing/sizing, that's all that I can offer.
And the the C language lawyers can say that 'unsigned' is explicit. :(
---
~Randy
Quoted 3 lines · randy_dunlap
| Who deprecated it?
Not technically deprecated, just undesirable in the kernel sources.
Who claims that?
On Sat, 4 Jun 2005 12:28:25 +0100 Christoph Hellwig wrote:
| On Fri, Jun 03, 2005 at 11:04:36AM -0700, randy_dunlap wrote: | > | Who deprecated it? | > | > Not technically deprecated, just undesirable in the kernel sources. | | Who claims that?
I dunno, but I'm not religious about it.
It's certainly not in CodingStyle, but I'm sure that I'm not just dreaming it (not making it up), I've seen it in emails from some maintainers, but searching for /unsigned/ generates too many hits to follow.
---
~Randy
Quoted 10 lines · randy_dunlap
On Sat, 4 Jun 2005 12:28:25 +0100 Christoph Hellwig wrote:
|
| Who claims that?I dunno, but I'm not religious about it.
It's certainly not in CodingStyle, but I'm sure that I'm not just dreaming it (not making it up), I've seen it in emails from some maintainers, but searching for /unsigned/ generates too many hits to follow.
Which means, we can safely ignore this issue. If "some maintainers" really care, this thing will resurface.
Jörn
Signature · 2 lines
Homo Sapiens is a goal, not a description. -- unknown
On Sat, 4 Jun 2005 19:07:58 +0200 Jörn Engel wrote:
| On Sat, 4 June 2005 09:58:11 -0700, randy_dunlap wrote: | > On Sat, 4 Jun 2005 12:28:25 +0100 Christoph Hellwig wrote: | > | | > | Who claims that? | > | > I dunno, but I'm not religious about it. | > | > It's certainly not in CodingStyle, but I'm sure that I'm not just | > dreaming it (not making it up), I've seen it in emails from some | > maintainers, but searching for /unsigned/ generates too many hits | > to follow. | | Which means, we can safely ignore this issue. If "some maintainers" | really care, this thing will resurface.
Certainly.
---
~Randy