Skip to content

bug: parsing large files/xml #149

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

Merged
merged 2 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions formatter/file.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package formatter

import (
"bufio"
"errors"
"io"
"os"
)
Expand All @@ -20,21 +18,6 @@ type InputFileConfig struct {
Source io.ReadCloser
}

// ReadContents reads content from stdin or provided file-path
func (i *InputFileConfig) ReadContents() ([]byte, error) {
var err error
var content []byte
if i.Source == nil {
return nil, errors.New("no reading source is defined")
}
scanner := bufio.NewScanner(i.Source)
for scanner.Scan() {
content = append(content, scanner.Bytes()...)
}
err = scanner.Err()
return content, err
}

// ExistsOpen tries to open a file for reading, returning an error if it fails
func (i *InputFileConfig) ExistsOpen() error {
f, err := os.Open(i.Path)
Expand Down
13 changes: 8 additions & 5 deletions formatter/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,15 @@ func (w *MainWorkflow) Execute() (err error) {

// parse reads & unmarshalles the input file into NMAPRun struct
func (w *MainWorkflow) parse() (run NMAPRun, err error) {
input, err := w.Config.InputFileConfig.ReadContents()
if err != nil {
return
if w.Config.InputFileConfig.Source == nil {
return run, fmt.Errorf("no input file is defined")
}
if err = xml.Unmarshal(input, &run); err != nil {
d := xml.NewDecoder(w.Config.InputFileConfig.Source)
_, err = d.Token()
if err != nil {
return
}
return run, nil

err = d.Decode(&run)
return
}
10 changes: 10 additions & 0 deletions formatter/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ func TestMainWorkflow_parse(t *testing.T) {
<nmaprun></nmaprun>`,
fileName: "main_workflow_parse_3_test",
},
{
name: "Bad XML file",
w: &MainWorkflow{
Config: &Config{},
},
wantNMAPRun: NMAPRun{},
wantErr: true,
fileContent: "<?x< version=",
fileName: "main_workflow_parse_4_test_wrong_xml",
},
{
name: "XML file with some matching output",
w: &MainWorkflow{
Expand Down