Comments (4)
I had a similar problem with the UP checks. Conditional UNPROTECT/PROTECT in loops do not work well for rchk
. However, in this case (at least looking at parseArray
), it seems you don't need to be incrementing the protection counter at all, as you'll only add one object to the PROTECT stack.
What I would do that I think will fix the problem is do one PROTECT_WITH_INDEX
outside of the loop on a dummy SEXP (e.g. R_NilValue
), REPROTECT
inside the loop, and instead of UNPROTECT
inside the loop, set a failure flag, break out of the loop. You can then just run the existing code if the failure flag is not set (and skip it if not), and UNPROTECT
once at the very end since the failure will not affect how many things you have on the protect stack (either the dummy object, or whatever you put on there in the loop).
from rjson.
I made some changes in https://github.com/alexcb/rjson/tree/simplify-protection-logic which gets the warnings down to a single imbalance:
#output from running: earthly -i +rcheck
...
+rcheck | Function parseArray
+rcheck | [PB] has possible protection stack imbalance /rchk/packages/build/Wc7jXdEM/rjson/src/parser.c:611
+rcheck | Analyzed 166 functions, traversed 1445 states.
I'm scratching my head on what's causing this one.
from rjson.
My 2c of advice is to forsake the seeming convenience of the early return. It makes keeping track of the logic harder, and it makes it easier to introduce bugs later even if the code is correct when first written (e.g. you have one return that needs to UNPROTECT(2) vs the others; if you add more code later you need to make sure that remains right everywhere).
I reviewed the code and at first I thought it was balanced, although it is complicated enough I wouldn't have bet my life on it. Then, I searched for each return statement in the function, and noticed that this return is unbalanced. There is the original PROTECT left that needs to be UNPROTECT(1), AFAICT (untested, so I could be wrong).
I don't trust myself to be able to rigorously maintain multiple exit points from my functions, so I very rarely use multiple returns, even though I am often tempted to do so.
Even if the code does turn out to be balanced (and I'm wrong), it still worth writing simpler code that rchk
can understand so that when it does issue a true positive it can be fixed (rchk
just saved my bacon on the submission I'm preparing).
from rjson.
Great eyes @brodieG, that was a bug I introduced with this refactor. It's been fixed in bb313d7
I agree it's complicated code to look over -- I wrote it over a decade ago, and find myself scratching my head at times.
so I very rarely use multiple returns
perhaps having an error
label at the end of the function with a goto error
could make life easier; i've used that pattern in several other C projects. Perhaps down the road this might be useful, but I think keeping it as-is for the time being is less risky.
Thank you very much for helping me get this cleaned up to the state where I can try a new update-submission to CRAN.
from rjson.
Related Issues (20)
- Add a simplify option to fromJSON to keep json list structure HOT 2
- fromJSON does not error on multiple json objects
- fromJSON: Manually set how null is handled (like jsonlite) HOT 1
- wired result (with NA involved) HOT 1
- Convert strings with javascript escaped unicode properly HOT 5
- unsupported SEXPTYPE: 19 HOT 6
- Feature request: a multiline JSON output HOT 6
- Feature request: integer types HOT 4
- [minor] couple of compiler warnings HOT 1
- [feature request] support for bit64::integer64 HOT 1
- fromJSON successfully parses incorrect test HOT 1
- incorrect simplify docs
- investigate "String is not utf8" error on what appears to be valid utf8 data HOT 1
- convert emoji characters. HOT 1
- `fromJSON` Crash With Bad Input HOT 1
- not all data parsed check introduces regression
- v3.6.3 version? HOT 2
- Read smart contacts with rjson HOT 4
- Enhancement: Import Just a Subset of a JSON File HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rjson.