[llvm-bugs] [Bug 31661] New: fixit hints: don't suggest initialization with 0 if var is known to be divisor

via llvm-bugs llvm-bugs at lists.llvm.org
Mon Jan 16 13:05:34 PST 2017


https://llvm.org/bugs/show_bug.cgi?id=31661

            Bug ID: 31661
           Summary: fixit hints: don't suggest initialization with 0 if
                    var is known to be divisor
           Product: clang
           Version: 3.9
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Frontend
          Assignee: unassignedclangbugs at nondot.org
          Reporter: matthias.krueger at famsik.de
                CC: llvm-bugs at lists.llvm.org
    Classification: Unclassified

This is not a big issue, I just found it a little strange:

this code:

static u64 get_dev_extent_len(struct map_lookup *map)
{
    int div;

    switch (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
    case 0: /* Single */
    case BTRFS_BLOCK_GROUP_DUP:
    case BTRFS_BLOCK_GROUP_RAID1:
        div = 1;
        break;
    case BTRFS_BLOCK_GROUP_RAID5:
        div = (map->num_stripes - 1);
        break;
    case BTRFS_BLOCK_GROUP_RAID6:
        div = (map->num_stripes - 2);
        break;
    case BTRFS_BLOCK_GROUP_RAID10:
        div = (map->num_stripes / map->sub_stripes);
        break;
    default:
        /* normally, read chunk security hook should handled it */
        BUG_ON(1);
    }
    return map->ce.size / div;
}
from btrfs
https://github.com/kdave/btrfs-progs/blob/bde687343b827bd425e912136ecf191c593b38fd/extent-tree.c#L3669

produces this warning:

extent-tree.c:3688:2: warning: variable 'div' is used uninitialized whenever
switch default is taken [-Wsometimes-uninitialized]
        default:
        ^~~~~~~
extent-tree.c:3692:24: note: uninitialized use occurs here
        return map->ce.size / div;
                              ^~~
extent-tree.c:3671:9: note: initialize the variable 'div' to silence this
warning
        int div;
               ^
                = 0


So if we initialized with 0 and the default case was taken (which should not
happen in theory), we would have a division by zero.
Perhaps clang could suggest initializing with "1" if the variable is known to
have uninitialized access as divisor otherwise?

Just an idea, feel free to close if you think this ticket is nonsense.

clang 3.9 and dev(to-be 4.0)

Kind regards
Matthias

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20170116/8d068c0f/attachment.html>


More information about the llvm-bugs mailing list