diff --git a/errors.go b/errors.go index d428c96..70fc439 100644 --- a/errors.go +++ b/errors.go @@ -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") diff --git a/nmap.go b/nmap.go index b4897f4..d6f7df5 100644 --- a/nmap.go +++ b/nmap.go @@ -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())