Avoid unnecessary reads when validating Scriptorium outputs

This commit is contained in:
2026-11-26 13:51:06 +00:00
parent 72eb597a72
commit 0a43b67847
3 changed files with 70 additions and 10 deletions

View File

@@ -202,13 +202,13 @@ func (r *SubprocessRunner) RenderArtifact(ctx context.Context, req RenderArtifac
if runErr != nil {
return result, fmt.Errorf("run scriptorium render (binary=%q): %w", req.Binary, runErr)
}
if err := validateNonEmptyOutput(req.OutputPath); err != nil {
return result, fmt.Errorf("validate scriptorium render output %q: %w", req.OutputPath, err)
}
data, err := fileops.ReadFileBounded(req.OutputPath, MaxOutputFileBytes)
if err != nil {
return result, fmt.Errorf("read scriptorium render output %q: %w", req.OutputPath, err)
}
if len(data) == 0 {
return result, fmt.Errorf("validate scriptorium render output %q: file is empty", req.OutputPath)
}
if !json.Valid(data) {
return result, fmt.Errorf("validate scriptorium render output %q: invalid JSON", req.OutputPath)
}
@@ -370,11 +370,17 @@ func writeInvocationConfig(path string, payload invocationPayload) error {
}
func validateNonEmptyOutput(path string) error {
data, err := fileops.ReadFileBounded(path, MaxOutputFileBytes)
info, err := os.Stat(path)
if err != nil {
return fmt.Errorf("scriptorium artifact output exceeds or cannot be read within %d-byte limit: %w", MaxOutputFileBytes, err)
return fmt.Errorf("stat scriptorium artifact output: %w", err)
}
if len(data) == 0 {
if !info.Mode().IsRegular() {
return fmt.Errorf("scriptorium artifact output is not a regular file")
}
if info.Size() > MaxOutputFileBytes {
return fmt.Errorf("scriptorium artifact output exceeds %d-byte limit", MaxOutputFileBytes)
}
if info.Size() == 0 {
return fmt.Errorf("file is empty")
}
return nil