Fix error: conversion from ‘int’ to ‘uchar’ {aka ‘unsigned char’} #120879

Closed
Giang-Chaebol wants to merge 1 commits from Giang-Chaebol/blender:FixConversionBlfGlyph into blender-v4.1-release

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
Contributor

This pull request fixes the following errors when following this instruction:

blender/source/blender/blenfont/intern/blf_glyph.cc:306:64: error: conversion from ‘int’ to ‘uchar’ {aka ‘unsigned char’} may change value [-Werror=conversion]
  306 |         g->bitmap[i] = blf_glyph_gamma(glyph->bitmap.buffer[i] * scale);
This pull request fixes the following errors when following [this](https://developer.blender.org/docs/handbook/building_blender/linux/#update-and-build) instruction: ``` blender/source/blender/blenfont/intern/blf_glyph.cc:306:64: error: conversion from ‘int’ to ‘uchar’ {aka ‘unsigned char’} may change value [-Werror=conversion] 306 | g->bitmap[i] = blf_glyph_gamma(glyph->bitmap.buffer[i] * scale); ```
Giang-Chaebol added 1 commit 2024-04-21 15:15:02 +02:00
Iliya Katushenock requested review from Harley Acheson 2024-04-21 15:15:41 +02:00
Member

Hello, and thanks for looking at this. My compiler is not giving me this same warning, so I am a bit blind here.

This warning is about an assignment to uchar from a function that returns uchar. That function takes one uchar argument and we are giving it the product of multiplying a uchar with a char.

So to my eye your proposed solution of a functional "char" cast, should actually be a functional "uchar" cast? But isn't the real problem that "scale", defined a few lines earlier, should be defined as uchar?

Does the following remove this warning for you?

diff --git a/source/blender/blenfont/intern/blf_glyph.cc b/source/blender/blenfont/intern/blf_glyph.cc
index 471b0efe046..bdba076277c 100644
--- a/source/blender/blenfont/intern/blf_glyph.cc
+++ b/source/blender/blenfont/intern/blf_glyph.cc
@@ -281,11 +281,11 @@ static GlyphBLF *blf_glyph_cache_add_glyph(FontBLF *font,
              FT_PIXEL_MODE_GRAY,
              FT_PIXEL_MODE_GRAY2,
              FT_PIXEL_MODE_GRAY4))
     {
       /* Scale 1, 2, 4-bit gray to 8-bit. */
-      const char scale = char(255 / (glyph->bitmap.num_grays - 1));
+      const uchar scale = uchar(255 / (glyph->bitmap.num_grays - 1));
       for (int i = 0; i < buffer_size; i++) {
 #ifdef BLF_GAMMA_CORRECT_GLYPHS
         /* Convert coverage amounts to perceptually-improved lightness values. */
         g->bitmap[i] = blf_glyph_gamma(glyph->bitmap.buffer[i] * scale);
 #else

Hello, and thanks for looking at this. My compiler is not giving me this same warning, so I am a bit blind here. This warning is about an assignment to uchar from a function that returns uchar. That function takes one uchar argument and we are giving it the product of multiplying a uchar with a char. So to my eye your proposed solution of a functional "char" cast, should actually be a functional "uchar" cast? But isn't the real problem that "scale", defined a few lines earlier, should be defined as uchar? Does the following remove this warning for you? ```Diff diff --git a/source/blender/blenfont/intern/blf_glyph.cc b/source/blender/blenfont/intern/blf_glyph.cc index 471b0efe046..bdba076277c 100644 --- a/source/blender/blenfont/intern/blf_glyph.cc +++ b/source/blender/blenfont/intern/blf_glyph.cc @@ -281,11 +281,11 @@ static GlyphBLF *blf_glyph_cache_add_glyph(FontBLF *font, FT_PIXEL_MODE_GRAY, FT_PIXEL_MODE_GRAY2, FT_PIXEL_MODE_GRAY4)) { /* Scale 1, 2, 4-bit gray to 8-bit. */ - const char scale = char(255 / (glyph->bitmap.num_grays - 1)); + const uchar scale = uchar(255 / (glyph->bitmap.num_grays - 1)); for (int i = 0; i < buffer_size; i++) { #ifdef BLF_GAMMA_CORRECT_GLYPHS /* Convert coverage amounts to perceptually-improved lightness values. */ g->bitmap[i] = blf_glyph_gamma(glyph->bitmap.buffer[i] * scale); #else ```
Author
Contributor

Hello, and thanks for looking at this. My compiler is not giving me this same warning, so I am a bit blind here.

This warning is about an assignment to uchar from a function that returns uchar. That function takes one uchar argument and we are giving it the product of multiplying a uchar with a char.

So to my eye your proposed solution of a functional "char" cast, should actually be a functional "uchar" cast? But isn't the real problem that "scale", defined a few lines earlier, should be defined as uchar?

Does the following remove this warning for you?

diff --git a/source/blender/blenfont/intern/blf_glyph.cc b/source/blender/blenfont/intern/blf_glyph.cc
index 471b0efe046..bdba076277c 100644
--- a/source/blender/blenfont/intern/blf_glyph.cc
+++ b/source/blender/blenfont/intern/blf_glyph.cc
@@ -281,11 +281,11 @@ static GlyphBLF *blf_glyph_cache_add_glyph(FontBLF *font,
              FT_PIXEL_MODE_GRAY,
              FT_PIXEL_MODE_GRAY2,
              FT_PIXEL_MODE_GRAY4))
     {
       /* Scale 1, 2, 4-bit gray to 8-bit. */
-      const char scale = char(255 / (glyph->bitmap.num_grays - 1));
+      const uchar scale = uchar(255 / (glyph->bitmap.num_grays - 1));
       for (int i = 0; i < buffer_size; i++) {
 #ifdef BLF_GAMMA_CORRECT_GLYPHS
         /* Convert coverage amounts to perceptually-improved lightness values. */
         g->bitmap[i] = blf_glyph_gamma(glyph->bitmap.buffer[i] * scale);
 #else

It still gives me the error, see the video.

image

> Hello, and thanks for looking at this. My compiler is not giving me this same warning, so I am a bit blind here. > > This warning is about an assignment to uchar from a function that returns uchar. That function takes one uchar argument and we are giving it the product of multiplying a uchar with a char. > > So to my eye your proposed solution of a functional "char" cast, should actually be a functional "uchar" cast? But isn't the real problem that "scale", defined a few lines earlier, should be defined as uchar? > > Does the following remove this warning for you? > > ```Diff > diff --git a/source/blender/blenfont/intern/blf_glyph.cc b/source/blender/blenfont/intern/blf_glyph.cc > index 471b0efe046..bdba076277c 100644 > --- a/source/blender/blenfont/intern/blf_glyph.cc > +++ b/source/blender/blenfont/intern/blf_glyph.cc > @@ -281,11 +281,11 @@ static GlyphBLF *blf_glyph_cache_add_glyph(FontBLF *font, > FT_PIXEL_MODE_GRAY, > FT_PIXEL_MODE_GRAY2, > FT_PIXEL_MODE_GRAY4)) > { > /* Scale 1, 2, 4-bit gray to 8-bit. */ > - const char scale = char(255 / (glyph->bitmap.num_grays - 1)); > + const uchar scale = uchar(255 / (glyph->bitmap.num_grays - 1)); > for (int i = 0; i < buffer_size; i++) { > #ifdef BLF_GAMMA_CORRECT_GLYPHS > /* Convert coverage amounts to perceptually-improved lightness values. */ > g->bitmap[i] = blf_glyph_gamma(glyph->bitmap.buffer[i] * scale); > #else > > ``` > > > It still gives me the error, see the video. ![image](/attachments/9ea178a8-1747-46ae-be70-7c8dbbbee100)
Member

It still gives me the error, see the video.

Sorry, but I'm not seeing the error in that video. Although you have blf_glyph.cc loaded, the capture is showing terminal messages to do with index_mask.cc

> It still gives me the error, see the video. Sorry, but I'm not seeing the error in that video. Although you have blf_glyph.cc loaded, the capture is showing terminal messages to do with index_mask.cc
Author
Contributor

It still gives me the error, see the video.

Sorry, but I'm not seeing the error in that video. Although you have blf_glyph.cc loaded, the capture is showing terminal messages to do with index_mask.cc

Yes sorry my bad. Hereby I send you the correct one.

image

> > It still gives me the error, see the video. > > Sorry, but I'm not seeing the error in that video. Although you have blf_glyph.cc loaded, the capture is showing terminal messages to do with index_mask.cc Yes sorry my bad. Hereby I send you the correct one. ![image](/attachments/1576b5df-d64e-4f7c-99b5-d1e70a0ee320)

@Giang-Chaebol it's strange that your running into these errors as I don't get them with recent Linux/macOS build environments, what compiler version are you using?

@Giang-Chaebol it's strange that your running into these errors as I don't get them with recent Linux/macOS build environments, what compiler version are you using?

In the build output there is the message "unrecognized command line option" this is a hint that there is something wrong with the build as CMake is meant to detect this flag and only pass it in when it's supported (see: C_WARN_NO_STRINGOP_OVERREAD, CXX_WARN_NO_STRINGOP_OVERREAD).

Could you check if creating a new build directory resolves the issue?

In the build output there is the message "unrecognized command line option" this is a hint that there is something wrong with the build as CMake is meant to detect this flag and only pass it in when it's supported (see: C_WARN_NO_STRINGOP_OVERREAD, CXX_WARN_NO_STRINGOP_OVERREAD). Could you check if creating a new build directory resolves the issue?
Author
Contributor

@Giang-Chaebol it's strange that your running into these errors as I don't get them with recent Linux/macOS build environments, what compiler version are you using?

(multiverse) giangnguyen@giangnguyen-PC:~$ g++ --version
g++ (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

(multiverse) giangnguyen@giangnguyen-PC:~$ cmake --version
cmake version 3.29.2

CMake suite maintained and supported by Kitware (kitware.com/cmake).
(multiverse) giangnguyen@giangnguyen-PC:~$ make --version
GNU Make 4.2.1
Built for x86_64-pc-linux-gnu
Copyright (C) 1988-2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
> @Giang-Chaebol it's strange that your running into these errors as I don't get them with recent Linux/macOS build environments, what compiler version are you using? ``` (multiverse) giangnguyen@giangnguyen-PC:~$ g++ --version g++ (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0 Copyright (C) 2019 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. (multiverse) giangnguyen@giangnguyen-PC:~$ cmake --version cmake version 3.29.2 CMake suite maintained and supported by Kitware (kitware.com/cmake). (multiverse) giangnguyen@giangnguyen-PC:~$ make --version GNU Make 4.2.1 Built for x86_64-pc-linux-gnu Copyright (C) 1988-2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. ```
Author
Contributor

In the build output there is the message "unrecognized command line option" this is a hint that there is something wrong with the build as CMake is meant to detect this flag and only pass it in when it's supported (see: C_WARN_NO_STRINGOP_OVERREAD, CXX_WARN_NO_STRINGOP_OVERREAD).

Could you check if creating a new build directory resolves the issue?

I always build from scratch, I even try to build it in a docker image and the same errors still appear.

> In the build output there is the message "unrecognized command line option" this is a hint that there is something wrong with the build as CMake is meant to detect this flag and only pass it in when it's supported (see: C_WARN_NO_STRINGOP_OVERREAD, CXX_WARN_NO_STRINGOP_OVERREAD). > > Could you check if creating a new build directory resolves the issue? I always build from scratch, I even try to build it in a docker image and the same errors still appear.

@Giang-Chaebol building shouldn't even have started since CMake checks GCC is at least 11.0.0, did you modify CMakeLists.txt in order to get Blender building?

What does gcc --version report?

@Giang-Chaebol building shouldn't even have started since CMake checks GCC is at least 11.0.0, did you modify `CMakeLists.txt` in order to get Blender building? What does `gcc --version` report?
Author
Contributor

@Giang-Chaebol building shouldn't even have started since CMake checks GCC is at least 11.0.0, did you modify CMakeLists.txt in order to get Blender building?

What does gcc --version report?

(multiverse) giangnguyen@giangnguyen-PC:~$ gcc --version
gcc (Ubuntu 11.4.0-2ubuntu1~20.04) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

> @Giang-Chaebol building shouldn't even have started since CMake checks GCC is at least 11.0.0, did you modify `CMakeLists.txt` in order to get Blender building? > > What does `gcc --version` report? (multiverse) giangnguyen@giangnguyen-PC:~$ gcc --version gcc (Ubuntu 11.4.0-2ubuntu1~20.04) 11.4.0 Copyright (C) 2021 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Author
Contributor

@Giang-Chaebol building shouldn't even have started since CMake checks GCC is at least 11.0.0, did you modify CMakeLists.txt in order to get Blender building?

What does gcc --version report?

And no, I don't modify anything.

> @Giang-Chaebol building shouldn't even have started since CMake checks GCC is at least 11.0.0, did you modify `CMakeLists.txt` in order to get Blender building? > > What does `gcc --version` report? And no, I don't modify anything.

@Giang-Chaebol I've committed additional checks for C++ version e5dfc814e9, can you check that the build now raises an error early on?

@Giang-Chaebol I've committed additional checks for C++ version e5dfc814e9c46f35c12fc7638bb6926d684dd2a3, can you check that the build now raises an error early on?
Author
Contributor

@Giang-Chaebol I've committed additional checks for C++ version e5dfc814e9, can you check that the build now raises an error early on?

Ok the error from your CMakeLists.txt appeared to stop me from building it. I upgraded my g++ to 11 and rebuilt it. Now it can compile fine without giving me any errors.

Btw, your message is confusing:

if("${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS "11.0.0")
    message(FATAL_ERROR "\
The minimum supported version of GCC is 11.0.0, found ${CMAKE_CXX_COMPILER_VERSION}"
    )
  endif()

instead of GCC, it should be G++, otherwise it doesn't make sense.

> @Giang-Chaebol I've committed additional checks for C++ version e5dfc814e9c46f35c12fc7638bb6926d684dd2a3, can you check that the build now raises an error early on? Ok the error from your `CMakeLists.txt` appeared to stop me from building it. I upgraded my `g++` to 11 and rebuilt it. Now it can compile fine without giving me any errors. Btw, your message is confusing: ``` if("${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS "11.0.0") message(FATAL_ERROR "\ The minimum supported version of GCC is 11.0.0, found ${CMAKE_CXX_COMPILER_VERSION}" ) endif() ``` instead of GCC, it should be G++, otherwise it doesn't make sense.
Author
Contributor

However, I still think it might be better to fix the line for cleaner code. Explicit type conversion prevents data loss and increases clarity.

However, I still think it might be better to fix the line for cleaner code. Explicit type conversion prevents data loss and increases clarity.

Btw, your message is confusing:

if("${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS "11.0.0")
    message(FATAL_ERROR "\
The minimum supported version of GCC is 11.0.0, found ${CMAKE_CXX_COMPILER_VERSION}"
    )
  endif()

instead of GCC, it should be G++, otherwise it doesn't make sense.

GCC is the name of the project, g++ is the command but this is still typically referred to as the GCC version.

Clarified between C/C++ compilers 5f6516a19a.

> Btw, your message is confusing: > > ``` > if("${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS "11.0.0") > message(FATAL_ERROR "\ > The minimum supported version of GCC is 11.0.0, found ${CMAKE_CXX_COMPILER_VERSION}" > ) > endif() > ``` > > instead of GCC, it should be G++, otherwise it doesn't make sense. GCC is the name of the project, `g++` is the command but this is still typically referred to as the GCC version. Clarified between C/C++ compilers 5f6516a19acd821b9bfb37321bfa257b8cca323d.

Closing this PR as the error was caused by an unsupported C++ compiler.

Our CMake compiler checks have been updated so this wont happen again.

Closing this PR as the error was caused by an unsupported C++ compiler. Our CMake compiler checks have been updated so this wont happen again.
Campbell Barton closed this pull request 2024-04-26 13:40:27 +02:00

Pull request closed

Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#120879
No description provided.