GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix indexing bug in ValueFactory by Minh-Ng · Pull Request #525 · msgpack/msgpack-java

Conversation

@Minh-Ng

@Minh-Ng Minh-Ng commented Jun 23, 2020

Copy link
Copy Markdown
Contributor

fix ValueFactory.newMap(Entry<Value, Value>...pairs), which is currently iterating by 2 when it should be iterating through each passed in pair.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Minh-Ng
fix indexing bug in constructor
b266f14
fix ValueFactory.newMap(Entry<Value, Value>...pairs), which is currently iterating by 2 when it should be iterating through each passed in pair.
@xerial

xerial commented Jun 23, 2020

Copy link
Copy Markdown
Member

I believe the original code is already correct because [i * 2] index is already used. Could you add a test case so that we can confirm your fix is valid?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Minh-Ng

Minh-Ng commented Jun 23, 2020

Copy link
Copy Markdown
Contributor Author

Test code on the project itself looks pretty light. In any case we can take a visual test case of
Map.Entry<? extends Value, ? extends Value> pairs = [Map.Entry<Value, Value> a, Map.Entry<Value, Value> b].

We want to process each pair, but the index you are referring to is filling the new array. So the for loop is actually trying to read non-existent indices of the pairs input array.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Minh-Ng

Minh-Ng commented Jun 23, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

In other words try and replace the for loop condition with

int fillIndex = 0;
Value[] newArray = new Value[pairs.length * 2];
for (Map.Entry<? extends Value, ? extends Value> entry : pairs) {
     newArray[fillIndex] = entry.getKey();
     newArray[fillIndex + 1] = entry.getValue();
     fillIndex += 2;
}

In the original code, if you pairs is 2 Entry objects, the second pair will not get read because the second iteration will have condition (i = 2) < pairs.length, since pairs.length == 2, this statement is false and the second pair is not copied. As a result the items at kvs[2] and kvs[3] will be null.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@xerial

xerial commented Jun 23, 2020

Copy link
Copy Markdown
Member

ah. Ok. You are right. I was looking at the wrong code. We need to iterate the original input one by one.

If you can add a very simple test for this, it will be helpful.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Minh-Ng

Minh-Ng commented Jun 23, 2020

Copy link
Copy Markdown
Contributor Author

Is it just me or there are no unit tests for src/test/java? I just came here from a nullpointerexception from an external library using messagepack core as a dependency. I'm not so familiar with this source.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Minh-Ng

Minh-Ng commented Jun 23, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author
Map.Entry<Value, Value> entry1 = new AbstractMap.SimpleImmutableEntry<>(ValueFactory.newInteger(10L), ValueFactory.newInteger(100L));
Map.Entry<Value, Value> entry2 = new AbstractMap.SimpleImmutableEntry<>(ValueFactory.newInteger(100L), ValueFactory.newInteger(10L));

MapValue newMap = ValueFactory.newMap(entry1, entry2);

String shouldNotThrowNPE = newMap.toString();
assertTrue(shouldNotThrowNPE != null && !"".equals(shouldNotThrowNPE));
assertEquals(2, newMap.asMapValue().entrySet().size())
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@xerial xerial self-assigned this Jun 23, 2020
@xerial
xerial merged commit 0348a49 into msgpack:develop Aug 31, 2020
@xerial

xerial commented Aug 31, 2020

Copy link
Copy Markdown
Member

Although some test is lacking, we should merge this fix. Thanks.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Minh-Ng
Minh-Ng deleted the patch-1 branch September 18, 2020 07:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@xerial xerial

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@Minh-Ng @xerial
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.