Skip to content

Commit 67803e3

Browse files
authored
handle invalid sections without segfaulting (#19)
1 parent 6d69b2b commit 67803e3

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

main.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ func main() {
105105
log.Fatal(err)
106106
}
107107

108-
aclSections := getAllowedSections(allowedAclSections, preDefinedAclSections)
108+
aclSections, err := getAllowedSections(allowedAclSections, preDefinedAclSections)
109+
if err != nil {
110+
log.Fatal(err)
111+
}
112+
109113
err = mergeDocs(aclSections, parentDoc, childDocs)
110114
if err != nil {
111115
log.Fatal(err)
@@ -114,13 +118,16 @@ func main() {
114118
outputFile(parentDoc.Object)
115119
}
116120

117-
func getAllowedSections(allowedAclSections []string, preDefinedAclSections map[string]SectionHandler) map[string]SectionHandler {
121+
func getAllowedSections(allowedAclSections []string, preDefinedAclSections map[string]SectionHandler) (map[string]SectionHandler, error) {
118122
aclSections := map[string]SectionHandler{}
119123
for _, v := range allowedAclSections {
124+
if preDefinedAclSections[v] == nil {
125+
return nil, fmt.Errorf("unsupported section [%s] specified in [-allow] flag", v)
126+
}
120127
aclSections[v] = preDefinedAclSections[v]
121128
}
122129
logVerbose("allowing ACL sections [%v]\n", aclSections)
123-
return aclSections
130+
return aclSections, nil
124131
}
125132

126133
type SectionHandler func(sectionKey string, parentPath string, parent *jwcc.Object, childPath string, childSection *jwcc.Member)

main_test.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,10 @@ func TestGetAllowedSections(t *testing.T) {
237237
"3": actualValue,
238238
}
239239
allowed := []string{"1", "2"}
240-
allowedAclSections := getAllowedSections(allowed, defined)
240+
allowedAclSections, err := getAllowedSections(allowed, defined)
241+
if err != nil {
242+
t.Fatalf("expected no error, got [%v]", err)
243+
}
241244

242245
// should exist
243246
section1 := allowedAclSections["1"]
@@ -260,6 +263,20 @@ func TestGetAllowedSections(t *testing.T) {
260263
}
261264
}
262265

266+
func TestGetAllowedSectionsInvalidSection(t *testing.T) {
267+
actualValue := handleObject()
268+
defined := map[string]SectionHandler{
269+
"1": actualValue,
270+
"2": actualValue,
271+
"3": actualValue,
272+
}
273+
allowed := []string{"1", "2", "invalid"}
274+
_, err := getAllowedSections(allowed, defined)
275+
if err == nil {
276+
t.Fatalf("expected error, got [%v]", err)
277+
}
278+
}
279+
263280
func TestHandleArray(t *testing.T) {
264281
parent, err := jwcc.Parse(strings.NewReader(ACL_PARENT))
265282
if err != nil {

0 commit comments

Comments
 (0)