Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why >>24 causes -Wconversion but >>23 doesn't?

Here is the code:

#include <stdint.h>

unsigned char f(uint32_t RGBA)
{
  return (RGBA>>24) & 0xFF;
}

When compiled with -Wconversion it causes "warning: conversion to ‘unsigned char’ from ‘uint32_t {aka unsigned int}’ may alter its value [-Wconversion]". If I lower the shift value to 23 or less the warning disappears.

I've looked through the C99 standard and I don't understand what happens here. If I remove the & operator then the warning is always emitted and that is probably good, as the result of the expression (after integer promotions) is larger than unsigned char. My only idea is that the warning is omitted for smaller shifts only because gcc is smart and sees that the result is 8-bit anyway, as the standard doesn't make this a special case. Am I right here?

And why does the shift value matter? Is that a GCC bug? Clang seems to not produce the warning for any shift values.

I'm using GCC 5.3.1 on a 64-bit Linux system.

like image 723
wRAR Avatar asked Jan 13 '16 19:01

wRAR


People also ask

What causes a segfault?

A segfault occurs when a reference to a variable falls outside the segment where that variable resides, or when a write is attempted to a location that is in a read-only segment.

How do I fix invalid syntax?

You can clear up this invalid syntax in Python by switching out the semicolon for a colon. Here, once again, the error message is very helpful in telling you exactly what is wrong with the line.

Why is my colon invalid syntax Python?

They usually indicate that there is something wrong with the syntax of the program. Example: Omitting the colon at the end of a def statement yields the somewhat redundant message SyntaxError: invalid syntax. Runtime errors are produced by the runtime system if something goes wrong while the program is running.

Why am I getting syntax errors in Python?

The Python SyntaxError occurs when the interpreter encounters invalid syntax in code. When Python code is executed, the interpreter parses it to convert it into bytecode. If the interpreter finds any invalid syntax during the parsing stage, a SyntaxError is thrown.


1 Answers

As mentioned by Shafik Yaghmour, this appears to be a bug in GCC:

GCC Bug 40752: -Wconversion generates false warnings for operands not larger than target type

It appears to have been present since version 4.4.0, first reported on 2009-07-14, and has 5 duplicates. Based on the comments in the bug report, there seems to be some debate on how to handle it.

like image 197
dbush Avatar answered Oct 22 '22 23:10

dbush