From d17ff2592abf80e71c208c786c2309f3a894620a Mon Sep 17 00:00:00 2001 From: xgopilot Date: Fri, 14 Nov 2025 11:37:09 +0000 Subject: [PATCH] build: improve error handling and code quality - Fix missing error handling in exportObject function - Add explicit warning for non-string variable rewrites - Improve documentation for maxRewriteValueLength constant Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: cpunion <8459+cpunion@users.noreply.github.com> --- cl/compile.go | 11 +++++++++-- internal/build/build.go | 7 ++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/cl/compile.go b/cl/compile.go index 807052c8..2abb36e4 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -235,8 +235,15 @@ func (p *context) compileGlobal(pkg llssa.Package, gbl *ssa.Global) { log.Println("==> NewVar", name, typ) } g := pkg.NewVar(name, typ, llssa.Background(vtype)) - if value, ok := p.rewriteValue(name); ok && p.isStringType(typ) { - g.Init(pkg.ConstString(value)) + if value, ok := p.rewriteValue(name); ok { + if p.isStringType(typ) { + g.Init(pkg.ConstString(value)) + } else { + log.Printf("warning: ignoring rewrite for non-string variable %s (type: %v)", name, typ) + if define { + g.InitNil() + } + } } else if define { g.InitNil() } diff --git a/internal/build/build.go b/internal/build/build.go index 99595667..2c7de4e4 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -631,6 +631,8 @@ var ( errXflags = errors.New("-X flag requires argument of the form importpath.name=value") ) +// maxRewriteValueLength limits the size of rewrite values to prevent +// excessive memory usage and potential resource exhaustion during compilation. const maxRewriteValueLength = 1 << 20 // 1 MiB cap per rewrite value func addGlobalString(conf *Config, arg string, mainPkgs []string) { @@ -1087,7 +1089,10 @@ func exportObject(ctx *context, pkgPath string, exportFile string, data []byte) if err != nil { return "", err } - f.Write(data) + if _, err := f.Write(data); err != nil { + f.Close() + return "", err + } err = f.Close() if err != nil { return exportFile, err