Skip to content

Removed a static variable used to contain the current recursion depth in json_reader.cpp #556

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

Merged
merged 2 commits into from
Dec 9, 2016

Conversation

nnkur
Copy link
Contributor

@nnkur nnkur commented Nov 30, 2016

Removed a static variable used to contain the current recursion depth of Reader::readValue(). The number of elements in an internal container Reader::nodes_ is used instead. It is correct because any recursive call of Reader::readValue() is executed with adjacent nodes_.push() and nodes_.pop() calls.
Added the option to change the allowed recursion depth at compile time by defining a macro JSONCPP_DEPRECATED_STACK_LIMIT as the required integer value.

Removed a static variable used to contain the current recursion depth of Reader::readValue().  The number of elements in an internal container Reader::nodes_  is used instead.  It is correct because any recursive call of Reader::readValue() is executed with adjacent nodes_.push()  and nodes_.pop() calls.  
Added the option to change the allowed recursion depth at compile time by defining a macro JSONCPP_STACK_LIMIT as the required integer value.
@nnkur nnkur closed this Nov 30, 2016
@nnkur nnkur deleted the nnkur-rec-fix branch November 30, 2016 16:12
@nnkur nnkur restored the nnkur-rec-fix branch December 5, 2016 13:06
@nnkur nnkur reopened this Dec 5, 2016
if (stackDepth_ >= features_.stackLimit_) throwRuntimeError("Exceeded stackLimit in readValue().");
++stackDepth_;
// To preserve the old behaviour we cast size_t to int.
if (static_cast<int>(nodes_.size()) > features_.stackLimit_) throwRuntimeError("Exceeded stackLimit in readValue().");
Copy link
Contributor

@cdunn2001 cdunn2001 Dec 6, 2016

Choose a reason for hiding this comment

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

I don't understand what was wrong with OurReader. Could you explain?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

stack<>.size() complexity is constant: stack is deque (http://www.cplusplus.com/reference/stack/stack/) and deque<>.size complexity - http://www.cplusplus.com/reference/deque/deque/size/

About OurReader. I have change it because of the following comment to the class:
// exact copy of Reader, renamed to OurReader
and
// complete copy of Read impl, for OurReader

If you allow some small differences I would rollback my changes to OurReader.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, it is ok for OurReader to differ.

Copy link
Contributor

@jia3ep jia3ep Dec 9, 2016

Choose a reason for hiding this comment

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

@cdunn2001 there's no sense to keep stackDepth_ in OurReader. New solution uses less additional memory (it's just a single member but I believe it's important for embedded devices) and still has the same computational complexity. And the tests are OK.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see your point. Thanks.

#else
static int stackDepth_g = 0; // see readValue()
// Define JSONCPP_STACK_LIMIT as an appropriate integer at compile time to change the stack limit
#if !defined(JSONCPP_STACK_LIMIT)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should be JSONCPP_DEPRECATED_STACK_LIMIT, since this should apply only to the old deprecated Reader, which nobody should be using anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

About JSONCPP_DEPRECATED_STACK_LIMIT: Ok. I will make the reqiured changes.
Actually there are some product, which used jsoncpp in the old manner. Replacing a dll is relatively simple, changing all modules may be considerable harder.

Renamed JSONCPP_STACK_LIMIT to JSONCPP_DEPRECATED_STACK_LIMIT to stress that usage of this macros assumes old interface.
@cdunn2001 cdunn2001 dismissed their stale review December 9, 2016 16:47

Good change.

@cdunn2001 cdunn2001 merged commit 0d25d9a into open-source-parsers:master Dec 9, 2016
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