Skip to content

Remove the call to 'abs' since unsigned values cannot be negative#55

Open
juergenhoetzel wants to merge 1 commit into
marc-q:masterfrom
juergenhoetzel:superfluous_abs
Open

Remove the call to 'abs' since unsigned values cannot be negative#55
juergenhoetzel wants to merge 1 commit into
marc-q:masterfrom
juergenhoetzel:superfluous_abs

Conversation

@juergenhoetzel

Copy link
Copy Markdown

The C11 standard states that for unsigned integers modulo wrapping is
the defined behavior and the term overflow never applies: "a
computation involving unsigned operands can never overflow."

Fixes warnings (clang):

ibbmp.c
../../home/juergen/shared/low-level-programming/sepia_filter/libbmp/libbmp.c:164:27: warning: taking the absolute value of unsigned type 'unsigned long' has no effect [-Wabsolute-value]
                fwrite (img->img_pixels[abs (offset - y)], sizeof (bmp_pixel), img->img_header.biWidth, img_file);
                                        ^
../../home/juergen/shared/low-level-programming/sepia_filter/libbmp/libbmp.c:164:27: note: remove the call to 'abs' since unsigned values cannot be negative
                fwrite (img->img_pixels[abs (offset - y)], sizeof (bmp_pixel), img->img_header.biWidth, img_file);
                                        ^~~
../../home/juergen/shared/low-level-programming/sepia_filter/libbmp/libbmp.c:210:30: warning: taking the absolute value of unsigned type 'unsigned long' has no effect [-Wabsolute-value]
                if (fread (img->img_pixels[abs (offset - y)], sizeof (bmp_pixel), items, img_file) != items)
                                           ^
../../home/juergen/shared/low-level-programming/sepia_filter/libbmp/libbmp.c:210:30: note: remove the call to 'abs' since unsigned values cannot be negative
                if (fread (img->img_pixels[abs (offset - y)], sizeof (bmp_pixel), items, img_file) != items)
                                           ^~~
2 warnings generated.

The C11 standard states that for unsigned integers modulo wrapping is
the defined behavior and the term overflow never applies: "a
computation involving unsigned operands can never overflow."
Comment thread libbmp.c
{
// Read a whole row of pixels from the file:
if (fread (img->img_pixels[abs (offset - y)], sizeof (bmp_pixel), items, img_file) != items)
if (fread (img->img_pixels[offset - y], sizeof (bmp_pixel), items, img_file) != items)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't functionally correct. If offset = 0 then the value wraps around starting from the max value of size_t

@0xBERNDOG 0xBERNDOG left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not functionally correct, see #62

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants