-
-
Notifications
You must be signed in to change notification settings - Fork 67
tighter %p default precision #340
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
Conversation
@stefano-zanotti-88 I'm on the fence about the current implementation- one of the things that keeps npf (relatively) simpler to reason about is that parse_format_spec returns processed + compatible flags. Check out the unit tests I had to comment out to get the build farther along: https://github.com/charlesnicholson/nanoprintf/pull/340/files#diff-194066f3e4aa5818e4ed73fcaed61d0a3a2b1961032b1bad5a5da2bfb2bef230 Is it possible to rework this such that parse_format_spec only returns legal combinations of flags? |
About npf_parse_format_spec(). Also: is this function meant to be user-accessible? If so, then maybe you might consider integrating into it the parsing of spans of literal characters. I mean, instead of returning 0 when a non-% or an error is found, it could return the length of the span, with A simple parsing without applying any semantic constraint is also useful for #298; otherwise, those safety checks don't have all the information needed for proper error detection (note that I'll have to rework #298 after the recent and future changes to NPF; some checks were indeed missing because of this reason). Also see #341 |
Might as well just delete the empty unit tests at this point :) Leaving the test cases around with all of the code commented out can be confusing to readers; if the claim is that the format spec can not be validated until star-args are read, and deleting the validation code is a core part of this PR, then would you mind also just fully deleting the tests that are half-commented-out please? |
This PR is authored by you, I don't think I can edit it. I would not delete the validation code. It just needs tweaks to check for all the options found in the format string, without any semantically-driven adjustment.
would become:
And this:
would become:
Of course, the subcase names would need some tweaking too. |
Ugh, I'm sorry, I forgot that we were discussing my PR 😬 Thanks for the feedback, these are good points and assumptions. I'll update the tests and then maybe you could take a final look at it and give me any thoughts. I do like this change! |
Sounds great. |
@stefano-zanotti-88 Looks like this PR regresses MSVC 64-bit. The failed run is here, could I ask you to take a look at it please? Everything else looks good at this point though! https://github.com/charlesnicholson/nanoprintf/actions/runs/15614590541/job/43983976675?pr=340
|
It might be related to #342, especially if the failed test is with a compilation done without
===== I'm unable to replicate it (on MinGW). Could you please add these tests in paland.cc:764?
This is meant to check (on other platforms too) whether the problem is with NPF itself, mishandling the precision, or whether it is a problem with the argument itself, getting truncated. You could also try and add this somewhere:
Compilation should fail, if the problem is indeed with MSVC's definition of builtin types. |
I added your new tests and they're failing on all platforms- check out the run here: https://github.com/charlesnicholson/nanoprintf/actions/runs/15669412831?pr=340
|
Sorry, the third test I gave you is wrong, it was meant to specify a precision too:
Since the first two new tests succeed (on other platforms at least), I'm quite sure the problem is that of #342. Lines 295 to 301 in bd3f1f7
with:
I think this will make the tests succeed; nothing else should break, since I don't think we have any test specifically targeting the condition |
I wasn't crazy about fully committing to uintmax and intmax because they can have huge performance penalties on smaller architectures, so I sat on this for a bit. I fixed the windows tests and AVR2/5 compilation tests with an alternate approach, please take a look- i'd love to hear your thoughts. Everything's green though, I'd like to land this! |
I wouldn't want to always use (u)intmax_t either. That was just a temporary change to pinpoint the problem, and indeed it seems that npf_int was the root cause. Adapting to the smallest size necessary is what I'd like too. Your latest commit causes no test failures, but I'm not sure it's correct.
That is, if long specifiers are disabled in NPF, we have no long long, size_t, etc., so the widest type we need to deal with is either intptr or long. Both must be supported, since %p and %li are never disabled in NPF.
Could you add it to the tests? I think this will fail on AVR, and the definition of npf_(u)int should be revised as a consequence. Again, see the proposed solution in #342 |
I don't actually have an AVR platform to execute on, and haven't set up anything like qemu, so the extent of the AVR test is just "do things compile". Re: the sizes, that makes sense, we want (edit: deleted the sizeof stuff; you're right) |
When it's at all possible, I prefer to probe the types using limits.h comparisons- I think this achieves what you're aiming at with the |
sizeof cannot be used in the preprocessor, unless by some compiler-specific extension, which is better avoided in a library like NPF. The macros in #342 came from some code that clang uses for a similar check, which I can't find anymore. Actually, we don't need the explicit values of the size of long and intptr, so the following should suffice:
|
That tidies up to this:
which looks pretty nice to me. Thanks for collaborating on this one, I'm (finally!) going to land it. |
Yes, this looks good. |
This PR is a port / migration of #329 by @stefano-zanotti-88 with changes to the paland + unit tests etc.