Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix warning sign compare #80

Merged
merged 6 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/include/CL/SDK/Image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace cl {
namespace sdk {
struct Image
{
int width = 0, height = 0, pixel_size = 1;
size_t width = 0, height = 0, pixel_size = 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

Given there are changes below to reinterpret_cast these back to int in one location below is this the correct change? Should the other side of whatever was triggering the warning be changed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reinterpret_cast was enforced by stb_image library which seams to be an old C-style code which uses signed int where unsigned int will suffice. As it is external library I could not change it. However Image class is a good place to set a boundary between new/old code.
Reinterpret cast is done while loading the image from file so there is no danger of loosing the data.
I would concern about writing image with stb as it is doing implicit cast from size_t to int.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems like a superficial problem to me. While on an academic level I agree with "don't use signed types where values can't be negative", as ill fate, 3rd parties and legacy C compatibility may have it, sometimes a cleanup is just too much hassle.

Yes, the SDK lib chose int to interface nicely with stb. Should we change the choice of lib in the future, because the SDK utilities don't leak to users, we are always free to reevaluate that choice. Your proposed changes while seem fine at first, it is an extreme level of "running with scissors". int and size_t aren't even the same size, so casting their pointers and expecting to get the right value ventures into endianness and two's complement territory and/or knowing how much our languages (and their particular versions) shield us from them. I really don't think this part of the code, which is otherwise warning-free without turning width into a size_t is worth the hassle to reason about such matters.

Minimally, it ought to be unsigned int to make sure that stb's choice of int is always matched at least in size on every arch. Yet again, while almost every other day I'd air on the side of choosing the most restrictive yet adequate number representation, I don't think this is the case where we should practice that.

cl::vector<unsigned char> pixels;
};

Expand Down
4 changes: 3 additions & 1 deletion lib/src/SDK/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ namespace sdk {

Image im;
unsigned char* data =
stbi_load(file_name, &im.width, &im.height, &im.pixel_size, 0);
stbi_load(file_name, reinterpret_cast<int*>(&im.width),
reinterpret_cast<int*>(&im.height),
reinterpret_cast<int*>(&im.pixel_size), 0);

if (data == nullptr)
{
Expand Down
2 changes: 1 addition & 1 deletion lib/src/Utils/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ cl_int cl::util::write_binaries(const cl::Program::Binaries& binaries,
{
try
{
for (auto i = 0; i < binaries.size(); ++i)
for (auto i = 0U; i < binaries.size(); ++i)
{
string binary_name = string(program_file_name) + "-"
+ devices.at(i).getInfo<CL_DEVICE_NAME>() + ".bin";
Expand Down
2 changes: 1 addition & 1 deletion samples/core/copybuffer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ int main(int argc, char** argv)
}
else
{
for (size_t i = 1; i < argc; i++)
for (int i = 1; i < argc; i++)
{
if (!strcmp(argv[i], "-d"))
{
Expand Down
2 changes: 1 addition & 1 deletion samples/core/copybufferkernel/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ int main(int argc, char** argv)
}
else
{
for (size_t i = 1; i < argc; i++)
for (int i = 1; i < argc; i++)
{
if (!strcmp(argv[i], "-d"))
{
Expand Down