Skip to content

deprecate some macros in tommath.h #223

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
wants to merge 6 commits into from
Closed

deprecate some macros in tommath.h #223

wants to merge 6 commits into from

Conversation

minad
Copy link
Member

@minad minad commented Apr 12, 2019

@sjaeckel Does it make sense to cleanup the public api a bit? This will only give warnings in gcc and probably clang. But I don't see a way to do this more generally.

Edit: I moved the MP_DEPRECATED to the public header since we might need it later. Furthermore I added MP_DEPRECATED_PRAGMA which can be used in macro situations.

@minad minad mentioned this pull request Apr 12, 2019
@minad minad force-pushed the deprecate-macros branch from 148e746 to 9858206 Compare April 12, 2019 20:56
@minad minad requested a review from sjaeckel April 12, 2019 20:56
@minad
Copy link
Member Author

minad commented Apr 12, 2019

@sjaeckel I squashed the two commits to avoid confusion.

@minad minad force-pushed the deprecate-macros branch from 9858206 to e87527f Compare April 12, 2019 21:06
@minad
Copy link
Member Author

minad commented Apr 16, 2019

@sjaeckel Ready to merge?

@minad minad requested a review from czurnieden April 25, 2019 09:33
Copy link
Contributor

@czurnieden czurnieden left a comment

Choose a reason for hiding this comment

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

Yepp, that's a good start, thanks!

PS: if you see another comment from me that doesn't make any sense/is completely outdated or similar: just ignore it, it is most likely just a public sign of me loosing another fight with Github's GUI which is definitely out to kill me! ;-)

@minad
Copy link
Member Author

minad commented Apr 25, 2019

There is only one issue I discovered - if the deprecated macros are used inside an #ifdef, the pragma blows up with a compile error instead of a warning.

An alternative solution for macro deprecation would be the following:

#ifndef MP_NO_DEPRECATED_MACROS
#  warning deprecated macros enabled, define MP_NO_DEPRECATED_MACROS!
#  define DIGIT_BITS MP_DIGIT_BITS
#endif

@czurnieden What do you think?

@czurnieden
Copy link
Contributor

the pragma blows up with a compile error instead of a warning

Looked good to me!?

What compiler with what error?

@sjaeckel
Copy link
Member

There is only one issue I discovered - if the deprecated macros are used inside an #ifdef, the pragma blows up with a compile error instead of a warning.

ew, that sounds nasty

how about creating a separate header file tommath_deprecated.h and include that one in tommath.h?

there then use this approach

#ifndef MP_NO_DEPRECATED_MACROS
#  warning deprecated macros enabled, define MP_NO_DEPRECATED_MACROS!
#  define DIGIT_BITS MP_DIGIT_BITS
#endif

and also protect the header file probably by #pragma once for compilers which support it + include guards for the rest!?

if it's in a separate file we can simply throw that file away in 2.0.0

after this is resolved the PR LGTM

@minad
Copy link
Member Author

minad commented Apr 25, 2019

@czurnieden This is a minor thing and a bit of a trade off what library users will see when they use deprecated macros.

Assume a library user does the following:

Alternative 1 (in this PR):

int a = DIGIT_BITS; /* compiles, but gives deprecation warning */
#if DIGIT_BITS < 32 /* compile error, no deprecation warning! */

Alternative 2 (with MP_NO_DEPRECATED_MACROS):

/* ALWAYS prints warning until user defines MP_NO_DEPRECATED_MACROS */
int a = DIGIT_BITS; /* compiles, no warning */
#if DIGIT_BITS < 32 /* compiles, no warning */

@minad
Copy link
Member Author

minad commented Apr 25, 2019

@sjaeckel Ok, I try to fix this.

minad added 4 commits April 25, 2019 13:39
* move MP_DEPRECATED to tommath.h since we need it later
* add MP_DEPRECATED_PRAGMA
Furthermore we need _MP_NO_DEPRECATED_WARNING to disable the warning on CI.
The macro is intentionally prefixed by an underscore to emphasise that
its only for internal usage.
@minad minad force-pushed the deprecate-macros branch from 7478417 to a079810 Compare April 25, 2019 11:51
@minad
Copy link
Member Author

minad commented Apr 25, 2019

@sjaeckel See my latest commit introducing the MP_NO_DEPRECATED macro. I didn't add additional headers. Grepping for DEPRECATED will show all the deprecated stuff when we are going to remove it.

@sjaeckel
Copy link
Member

The macro is intentionally prefixed by an underscore to emphasise that
its only for internal usage.

and what do we do with the C standard saying that this means it's UB?

How about calling it INTERNAL_MP_... instead?

@minad
Copy link
Member Author

minad commented Apr 25, 2019

The macro is intentionally prefixed by an underscore to emphasise that its only for internal usage.

Hmm, I don't think there is a problem. There are other places where I used a _ prefix. But I can change this since INTERNAL_ is a bit more clear.

@sjaeckel
Copy link
Member

Hmm, I don't think there is a problem. There are other places where I used a _ prefix

It's no real problem AFAICT, just the standard telling us what we're allowed to do and what not...

c.f. libtom/libtomcrypt#448

@@ -33,6 +25,10 @@ extern void *MP_CALLOC(size_t nmemb, size_t size);
extern void MP_FREE(void *mem, size_t size);
#endif

/* TODO: Remove PRIVATE_MP_WARRAY as soon as deprecated MP_WARRAY is removed from tommath.h */
Copy link
Member

Choose a reason for hiding this comment

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

👍

@minad
Copy link
Member Author

minad commented Apr 25, 2019

@sjaeckel Hmm, I played a bit with the patched version. I must admit that I don't really like the new solution since it always prints a warning even if nothing deprecated is used. I think this will produce much more annoyance for the users in contrast to Alternative 1, where breakage will only occur if DIGIT_BITS is used in an #ifdef, which is rather improbable.

The other possibility would be to just #define DIGIT_BITS MP_DIGIT_BITS, but this would then lead to breakage when DIGIT_BITS is removed at some point.

I don't think there is a perfect solution here. But I think Alternative 1 is the most reasonable version, which just breaks early but only in a few rare cases. #226 is the code for Alternative 1 without the MP_NO_DEPRECATED stuff.

@minad minad mentioned this pull request Apr 25, 2019
@minad
Copy link
Member Author

minad commented Apr 25, 2019

closed in favor of #226

@minad minad closed this Apr 25, 2019
@minad minad deleted the deprecate-macros branch November 14, 2019 14:34
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.

3 participants