Skip to content

Compiler warning for cast in Ticker code #6281

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

Closed
Niek opened this issue Jul 10, 2019 · 4 comments
Closed

Compiler warning for cast in Ticker code #6281

Niek opened this issue Jul 10, 2019 · 4 comments

Comments

@Niek
Copy link
Contributor

Niek commented Jul 10, 2019

Sample code:

#include <Ticker.h>

void testFn(unsigned char i) {}

void setup() {
  Ticker t;
  unsigned char i = 1;
  t.once_ms(100, testFn, i);
}

I get this warning when compiling the latest git version:

libraries/Ticker/Ticker.h: In instantiation of 'void Ticker::once_ms(uint32_t, void (*)(TArg), TArg) [with TArg = unsigned char; uint32_t = unsigned int]':

libraries/Ticker/Ticker.h:110:94: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
   _attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);

It originates from this line of code (changed in commit 7910121):

_attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);

It seems that it's caused by the (void*) cast in the last parameter.

@earlephilhower
Copy link
Collaborator

You have a problem in your code.

The function signature for a callback is void (cb)(void *data), not void (cb)(unsigned char). Change the callback to the proper signature and then, inside the function, convert from void* to the unsigned char with something like below.

void testFn(void *cbData) {
    unsigned char i = static_cast<unsigned char>(cbData);
   ....
}

@Niek
Copy link
Contributor Author

Niek commented Jul 10, 2019

Are you saying the callback function can't take other types as argument, as explained in the sample? https://github.com./esp8266/Arduino/blob/master/libraries/Ticker/examples/TickerParameter/TickerParameter.ino
Or is it the unsigned part that makes it problematic?
By the way the code works fine in core 2.5.2.

@earlephilhower
Copy link
Collaborator

Actually, I'm mistaken and you're right. The template is supposed to handle that from how I read it:

        template<typename TArg>
        void once_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg)

The (void*) in the template body looks to be incorrect and should be able to do promotion to a pointer silently, w/o error. We need a fix and a test case that hits this.

earlephilhower added a commit to earlephilhower/Arduino that referenced this issue Jul 10, 2019
Fixes errors seen in esp8266#6281 and adds a slight test case to the examples
to ensure no compiler errors.
devyte pushed a commit that referenced this issue Jul 11, 2019
Fixes errors seen in #6281 and adds a slight test case to the examples
to ensure no compiler errors.
@devyte devyte added this to the 2.6.0 milestone Jul 11, 2019
@Niek
Copy link
Contributor Author

Niek commented Jul 11, 2019

Thanks! I can confirm it's fixed in 8c37601 - closing this issue

@Niek Niek closed this as completed Jul 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants