Skip to content

Commit

Permalink
Added error type and enhanced the error checking (#45)
Browse files Browse the repository at this point in the history
* added error

Added check for warnings that would inevitably lead to parsing errors, hence, have higher priority

* Update errors.go

Added malloc error variable
  • Loading branch information
noneymous committed Mar 2, 2020
1 parent bdc60e5 commit 227aaf3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions errors.go
Expand Up @@ -13,6 +13,9 @@ var (
// ErrScanTimeout means that the provided context was done before the scanner finished its scan.
ErrScanTimeout = errors.New("nmap scan timed out")

// ErrMallocFailed means that nmap crashed due to insufficient memory, which may happen on large target networks.
ErrMallocFailed = errors.New("malloc failed, probably out of space")

// ErrParseOutput means that nmap's output was not parsed successfully.
ErrParseOutput = errors.New("unable to parse nmap output, see warnings for details")

Expand Down
10 changes: 10 additions & 0 deletions nmap.go
Expand Up @@ -97,6 +97,16 @@ func (s *Scanner) Run() (result *Run, warnings []string, err error) {
warnings = strings.Split(strings.Trim(stderr.String(), "\n"), "\n")
}

// Check for warnings that will inevitable lead to parsing errors, hence, have priority
for _, warning := range warnings {
switch {
case strings.Contains(warning, "Malloc Failed!"):
return nil, warnings, ErrMallocFailed
// TODO: Add cases for other known errors we might want to guard.
default:
}
}

// Parse nmap xml output. Usually nmap always returns valid XML, even if there is a scan error.
// Potentially available warnings are returned too, but probably not the reason for a broken XML.
result, err := Parse(stdout.Bytes())
Expand Down

0 comments on commit 227aaf3

Please sign in to comment.