Skip to content

Commit a9e78c2

Browse files
committed
bugfix: Kill compile processes that generates too much output
1 parent b4fc230 commit a9e78c2

File tree

1 file changed

+37
-4
lines changed
  • internal/arduino/builder/internal/preprocessor

1 file changed

+37
-4
lines changed

Diff for: internal/arduino/builder/internal/preprocessor/gcc.go

+37-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package preprocessor
1717

1818
import (
19+
"bytes"
1920
"context"
2021
"errors"
2122
"fmt"
@@ -77,10 +78,42 @@ func GCC(
7778
if err != nil {
7879
return Result{}, err
7980
}
80-
stdout, stderr, err := proc.RunAndCaptureOutput(ctx)
8181

82-
// Append gcc arguments to stdout
83-
stdout = append([]byte(fmt.Sprintln(strings.Join(args, " "))), stdout...)
82+
stdout := bytes.NewBuffer(nil)
83+
stderr := bytes.NewBuffer(nil)
8484

85-
return Result{args: proc.GetArgs(), stdout: stdout, stderr: stderr}, err
85+
ctx, cancel := context.WithCancel(ctx)
86+
defer cancel()
87+
count := 0
88+
stderrLimited := writerFunc(func(p []byte) (int, error) {
89+
// Limit the size of the stderr buffer to 100KB
90+
n, err := stderr.Write(p)
91+
count += n
92+
if count > 100*1024 {
93+
cancel()
94+
fmt.Fprintln(stderr, i18n.Tr("Compiler error output has been truncated."))
95+
}
96+
return n, err
97+
})
98+
99+
proc.RedirectStdoutTo(stdout)
100+
proc.RedirectStderrTo(stderrLimited)
101+
102+
// Append gcc arguments to stdout before running the command
103+
fmt.Fprintln(stdout, strings.Join(args, " "))
104+
105+
if err := proc.Start(); err != nil {
106+
return Result{}, err
107+
}
108+
109+
// Wait for the process to finish
110+
err = proc.WaitWithinContext(ctx)
111+
112+
return Result{args: proc.GetArgs(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
113+
}
114+
115+
type writerFunc func(p []byte) (n int, err error)
116+
117+
func (f writerFunc) Write(p []byte) (n int, err error) {
118+
return f(p)
86119
}

0 commit comments

Comments
 (0)