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

variable 'j' set but not used #1288

Open
jolaf opened this issue Aug 24, 2024 · 6 comments
Open

variable 'j' set but not used #1288

jolaf opened this issue Aug 24, 2024 · 6 comments

Comments

@jolaf
Copy link

jolaf commented Aug 24, 2024

Building cryptopp-cmake v8.9.0 with cmake v3.22.1 under Android Studio on Windows 10 produces the following warning:

C/C++: ninja: Entering directory `[...]'
C/C++: [...]/cryptopp-cmake/cryptopp/validat1.cpp:1053:31: warning: variable 'j' set but not used [-Wunused-but-set-variable]
C/C++:  1053 |             for (unsigned int j=0; !source.AnyRetrievable(); ++j)
C/C++:       |                               ^
C/C++: 1 warning generated.
@jolaf
Copy link
Author

jolaf commented Sep 18, 2024

I'm not sure why for loop is used, probably it should have been while (!source.AnyRetrievable()) ?

@EduardoMiravalls
Copy link

It looks like this variable is only used by a human to debug if the test TestHuffmanCodes crashes. If you run the test with gdb and it crashes, you can inspect the stack to check the value of j to know the byte that caused the crash without having to print it.

@jolaf
Copy link
Author

jolaf commented Dec 26, 2024

Maybe this variable could be renamed to something like _j that would not trigger warning?

@EduardoMiravalls
Copy link

Sorry, I'm not familiar with your setup and it might depend on your configuration or software versions. I don't get the warning with g++ 11.4.0. Have you tried renaming the variable to _j?
Assuming the compiler you are using is also gcc/g++, if you look at gcc's documentation about the warning, I would say the compiler should not care about the specific name of the variable unless the warning is suppressed by an argument or variable attribute.
If you see the warning disappears depending on the spelling of the variable, then I guess your IDE has some sort of macro or script that changes your compiler flags depending on your code. In any case, it is safe to ignore this warning.

@jolaf
Copy link
Author

jolaf commented Dec 27, 2024

No, renaming j to _j didn't help, as it would in, e .g., Python.

Probably there are some C/C++ specific ways to suppress the warning?

As far as I understand Android NDK uses clang compiler for C/C++.

@EduardoMiravalls
Copy link

Probably there are some C/C++ specific ways to suppress the warning?

You have to come up with ways to re-write the code so that the compiler detects j is used... or remove the j variable.
For example, you can try to add a statement that uses j but does nothing like (void)j; in the loop body (not sure if this will work in your setup as I can't reproduce the warning with clang 14.0.0 in a mwe):

for (unsigned int j=0; !source.AnyRetrievable(); ++j) {
	decoder.Decode(reader, val);
	(void)j; // try to suppress warning: variable 'j' set but not used
}

or you could change the loop condition to use j as it seems it should be less than the size of data1 (0xfff):

for (unsigned int j=0; !source.AnyRetrievable() && j < sizeof(data1); ++j)

If none of those work... you have to be more creative than me.

Another option is to disable the warning from the compiler, perhaps you could add -Wno-unused-but-set-variable to the GNUMakefile to disable the warning if clang supports it (if your clang version is at least 13 according to this random github issue I found). I would try adding it to the TCOMMAND or try to find the line that compiles cryptopp/validat1.cpp and add the flag, but this Makefile is fairly complex so I can't help you more there.

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

No branches or pull requests

2 participants