Skip to content

Dereferencing of the invalid iterator 'iter' might take place. #649

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
samthemadrabbit opened this issue Aug 18, 2017 · 4 comments
Closed

Comments

@samthemadrabbit
Copy link

samthemadrabbit commented Aug 18, 2017

PVS-Studio shows possible errors on lines 4704, 4920 and 5208 of amalgamated source file jsoncpp.cpp (version 1.8.1) for "Dereferencing of the invalid iterator 'iter' might take place."

@cdunn2001
Copy link
Contributor

Post a pull-request?

@BillyDonahue
Copy link
Contributor

I can't replicate this.
Can you identify the source locations or paste the lines?
I'm checking out branch 1.8.1 and my amalgamated dist/jsoncpp.cpp doesn't have any 'iter' on those line numbers.

@samthemadrabbit
Copy link
Author

samthemadrabbit commented Aug 28, 2017

These are in methods:

void StyledWriter::writeCommentBeforeValue(const Value& root) {
  if (!root.hasComment(commentBefore))
    return;

  document_ += "\n";
  writeIndent();
  const JSONCPP_STRING& comment = root.getComment(commentBefore);
  JSONCPP_STRING::const_iterator iter = comment.begin();
  while (iter != comment.end()) {
    document_ += *iter;
    if (*iter == '\n' &&
       (iter != comment.end() && *(iter + 1) == '/'))
      writeIndent();
    ++iter;
  }
void StyledStreamWriter::writeCommentBeforeValue(const Value& root) {
  if (!root.hasComment(commentBefore))
    return;

  if (!indented_) writeIndent();
  const JSONCPP_STRING& comment = root.getComment(commentBefore);
  JSONCPP_STRING::const_iterator iter = comment.begin();
  while (iter != comment.end()) {
    *document_ << *iter;
    if (*iter == '\n' &&
       (iter != comment.end() && *(iter + 1) == '/'))
      // writeIndent();  // would include newline
      *document_ << indentString_;
    ++iter;
  }
  indented_ = false;
}
void BuiltStyledStreamWriter::writeCommentBeforeValue(Value const& root) {
  if (cs_ == CommentStyle::None) return;
  if (!root.hasComment(commentBefore))
    return;

  if (!indented_) writeIndent();
  const JSONCPP_STRING& comment = root.getComment(commentBefore);
  JSONCPP_STRING::const_iterator iter = comment.begin();
  while (iter != comment.end()) {
    *sout_ << *iter;
    if (*iter == '\n' &&
       (iter != comment.end() && *(iter + 1) == '/'))
      // writeIndent();  // would write extra newline
      *sout_ << indentString_;
    ++iter;
  }
  indented_ = false;
}

All warnings are shown on the condition

if (*iter == '\n' &&
       (iter != comment.end() && *(iter + 1) == '/'))

cdunn2001 added a commit to cdunn2001/jsoncpp that referenced this issue Aug 28, 2017
@cdunn2001
Copy link
Contributor

That's theoretically possible, but only if the comment is illegal. Anyway, looks easy to prevent. Fixing...

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

No branches or pull requests

3 participants