Fix numeric configuration keys handling #198
Merged
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.
This is one of the ways to fix the issue with configuration parsing where the keys are numeric strings.
Couple of example of problem when creating configuration with NewFrom API call:
INPUT:
RESULT:
INPUT:
RESULT:
Solution
The library is quite old, so being overly cautious here in order to avoid breaking the original behavior. One of the possible approaches here is to preserve the same behavior as before, put some protection from large allocations and OOM, allow the user to enable a different treatment of the index (numeric) configuration fields.
Changes
Implement some sane, configurable (
MaxIdx
option) limit on the "index" field value, in order to prevent huge memory allocations and crashes. It's set to 1024 by default. The numeric field will be converted to the array as before only if the numberic value is less or equal to the max set. This is the first "line of defense" that is protecting the existing implementation from blowing up the process.Implement
EnableNumKeys
option in order to enable the "numeric keys" in the configuration. This allows the numeric keys and preserves it's value as is. This is disabled by default in order to preserve backwards compatibility.Add unit tests verifying the original problem is fixed and covering the new options.
P.S. Since the library is quite old looks like Go formatting change since then, so got some comments reindented on save.