cvs commit: src/sbin/badsect Makefile badsect.c

Joerg Sonnenberger joerg at britannica.bec.de
Sun Feb 13 23:48:18 PST 2005


On Sun, Feb 13, 2005 at 11:22:42AM -0800, Chris Pressey wrote:
> cpressey    2005/02/13 11:22:42 PST
> 
> DragonFly src repository
> 
>   Modified files:
>     sbin/badsect         Makefile badsect.c 
>   Log:
>   Raise WARNS to 6:
>   - Double-cast values when testing their ranges for validity.

@@ -142,7 +142,7 @@ main(int argc, char **argv)
                 * bit was lost by bogus sign extensions.
                 */
                diskbn = dbtofsb(fs, number);
-               if ((dev_t)diskbn != diskbn) {
+               if ((daddr_t)((dev_t)diskbn) != diskbn) {
                        printf("sector %ld cannot be represented as a dev_t\n",
                            (long)number);
                        errs++;

This is bad. The old code is evil, but that's even worse. Can you think
of a better way to test it?

@@ -163,7 +163,7 @@ chkuse(daddr_t blkno, int cnt)
        daddr_t fsbn, bn;

        fsbn = dbtofsb(fs, blkno);
-       if ((unsigned)(fsbn+cnt) > fs->fs_size) {
+       if ((int32_t)((unsigned)(fsbn+cnt)) > fs->fs_size) {
                printf("block %ld out of range of file system\n", (long)blkno);

This part is bogus. fsbn + cnt can overflow and the int32_t cast makes
it smaller than any non-negative fs->fs_size.

Joerg





More information about the Commits mailing list