Skip to content

Commit 44a4cb1

Browse files
authored
internal/ini: Fix ini parser to handle empty values (#406)
The ini parser incorrectly decided whether a statement should be skipped. As a result, valid statements in the ini files were being squashed. The PR fixes incorrect modifications to the previous token value of the skipper. We also add checks for cases where a skipped statement should be marked as complete and not be ignored. Adds test cases for cases for statements that need to be skipped. Also adds suggested tests from aws/aws-sdk-go#2801 .
1 parent 51d362f commit 44a4cb1

File tree

9 files changed

+91
-9
lines changed

9 files changed

+91
-9
lines changed

CHANGELOG_PENDING.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,6 @@ SDK Bugs
4040
---
4141
* `service/s3/s3manager`: Fix index out of range when a streaming reader returns -1 ([#378](https://github.com/aws/aws-sdk-go-v2/pull/378))
4242
* Fixes the S3 Upload Manager's handling of an unbounded streaming reader that returns negative bytes read.
43+
* `internal/ini`: Fix ini parser to handle empty values [#406](https://github.com/aws/aws-sdk-go-v2/pull/406)
44+
* Fixes incorrect modifications to the previous token value of the skipper. Adds checks for cases where a skipped statement should be marked as complete and not be ignored.
45+
* Adds tests for nested and empty field value parsing, along with tests suggested in [#2801](https://github.com/aws/aws-sdk-go/pull/2801)

internal/ini/ini_parser.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ loop:
162162
if len(tokens) == 0 {
163163
break loop
164164
}
165-
165+
// if should skip is true, we skip the tokens until should skip is set to false.
166166
step = SkipTokenState
167167
}
168168

@@ -218,7 +218,7 @@ loop:
218218
// S -> equal_expr' expr_stmt'
219219
switch k.Kind {
220220
case ASTKindEqualExpr:
221-
// assiging a value to some key
221+
// assigning a value to some key
222222
k.AppendChild(newExpression(tok))
223223
stack.Push(newExprStatement(k))
224224
case ASTKindExpr:
@@ -250,6 +250,13 @@ loop:
250250
if !runeCompare(tok.Raw(), openBrace) {
251251
return nil, NewParseError("expected '['")
252252
}
253+
// If OpenScopeState is not at the start, we must mark the previous ast as complete
254+
//
255+
// for example: if previous ast was a skip statement;
256+
// we should mark it as complete before we create a new statement
257+
if k.Kind != ASTKindStart {
258+
stack.MarkComplete(k)
259+
}
253260

254261
stmt := newStatement()
255262
stack.Push(stmt)

internal/ini/ini_parser_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,25 @@ region = us-west-2
268268
newExprStatement(noQuotesRegionEQRegion),
269269
},
270270
},
271+
{
272+
name: "missing section statement",
273+
r: bytes.NewBuffer([]byte(
274+
`[default]
275+
s3 =
276+
[assumerole]
277+
output = json
278+
`)),
279+
expectedStack: []AST{
280+
newCompletedSectionStatement(
281+
defaultProfileStmt,
282+
),
283+
newSkipStatement(newEqualExpr(newExpression(s3ID), equalOp)),
284+
newCompletedSectionStatement(
285+
assumeProfileStmt,
286+
),
287+
newExprStatement(outputEQExpr),
288+
},
289+
},
271290
}
272291

273292
for i, c := range cases {

internal/ini/skipper.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@ func newSkipper() skipper {
2222
}
2323

2424
func (s *skipper) ShouldSkip(tok Token) bool {
25+
// should skip state will be modified only if previous token was new line (NL);
26+
// and the current token is not WhiteSpace (WS).
2527
if s.shouldSkip &&
2628
s.prevTok.Type() == TokenNL &&
2729
tok.Type() != TokenWS {
28-
2930
s.Continue()
3031
return false
3132
}
32-
s.prevTok = tok
3333

34+
s.prevTok = tok
3435
return s.shouldSkip
3536
}
3637

3738
func (s *skipper) Skip() {
3839
s.shouldSkip = true
39-
s.prevTok = emptyToken
4040
}
4141

4242
func (s *skipper) Continue() {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[foo]
2+
aws_access_key_id =
3+
aws_secret_access_key =
4+
aws_session_token =
5+
[bar]
6+
aws_access_key_id = valid
7+
aws_secret_access_key = valid
8+
aws_session_token = valid
9+
[baz]
10+
aws_access_key_id = valid
11+
aws_secret_access_key = valid
12+
aws_session_token = valid
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"foo": {
3+
},
4+
"bar": {
5+
"aws_access_key_id": "valid",
6+
"aws_secret_access_key": "valid",
7+
"aws_session_token": "valid"
8+
},
9+
"baz": {
10+
"aws_access_key_id": "valid",
11+
"aws_secret_access_key": "valid",
12+
"aws_session_token": "valid"
13+
}
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[foo]
2+
aws_access_key_id =
3+
aws_secret_access_key = valid
4+
aws_session_token = valid
5+
[bar]
6+
aws_access_key_id = valid
7+
aws_secret_access_key = valid
8+
aws_session_token = valid
9+
[baz]
10+
aws_access_key_id = valid
11+
aws_secret_access_key = valid
12+
aws_session_token = valid
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"foo": {
3+
"aws_session_token": "valid"
4+
},
5+
"bar": {
6+
"aws_access_key_id": "valid",
7+
"aws_secret_access_key": "valid",
8+
"aws_session_token": "valid"
9+
},
10+
"baz": {
11+
"aws_access_key_id": "valid",
12+
"aws_secret_access_key": "valid",
13+
"aws_session_token": "valid"
14+
}
15+
}

internal/ini/walker_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,24 @@ func TestValidDataFiles(t *testing.T) {
6363
case string:
6464
a := p.String(k)
6565
if e != a {
66-
t.Errorf("%s: expected %v, but received %v", path, e, a)
66+
t.Errorf("%s: expected %v, but received %v for profile %v", path, e, a, profile)
6767
}
6868
case int:
6969
a := p.Int(k)
7070
if int64(e) != a {
71-
t.Errorf("%s: expected %v, but received %v", path, e, a)
71+
t.Errorf("%s: expected %v, but received %v for profile %v", path, e, a, profile)
7272
}
7373
case float64:
7474
v := p.values[k]
7575
if v.Type == IntegerType {
7676
a := p.Int(k)
7777
if int64(e) != a {
78-
t.Errorf("%s: expected %v, but received %v", path, e, a)
78+
t.Errorf("%s: expected %v, but received %v for profile %v", path, e, a, profile)
7979
}
8080
} else {
8181
a := p.Float64(k)
8282
if e != a {
83-
t.Errorf("%s: expected %v, but received %v", path, e, a)
83+
t.Errorf("%s: expected %v, but received %v for profile %v", path, e, a, profile)
8484
}
8585
}
8686
default:

0 commit comments

Comments
 (0)