Skip to content

Commit 0c440ad

Browse files
author
Christopher M. Wolff
authored
fix: add length check to avoid allocs checking for JSON "null" (#2679)
1 parent 889f84e commit 0c440ad

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

lang/compiler.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,19 @@ func wrapFileJSONInPkg(bs []byte) []byte {
171171
string(bs)))
172172
}
173173

174+
func IsNonNullJSON(bs json.RawMessage) bool {
175+
if len(bs) == 0 {
176+
return false
177+
}
178+
if len(bs) == 4 && string(bs) == "null" {
179+
return false
180+
}
181+
return true
182+
}
183+
174184
func (c FluxCompiler) Compile(ctx context.Context, runtime flux.Runtime) (flux.Program, error) {
175185
// Ignore context, it will be provided upon Program Start.
176-
if c.Extern != nil && string(c.Extern) != "null" {
186+
if IsNonNullJSON(c.Extern) {
177187
hdl, err := runtime.JSONToHandle(wrapFileJSONInPkg(c.Extern))
178188
if err != nil {
179189
return nil, err
@@ -208,7 +218,7 @@ func (c ASTCompiler) Compile(ctx context.Context, runtime flux.Runtime) (flux.Pr
208218
}
209219

210220
// Ignore context, it will be provided upon Program Start.
211-
if c.Extern != nil && string(c.Extern) != "null" {
221+
if IsNonNullJSON(c.Extern) {
212222
extHdl, err := runtime.JSONToHandle(wrapFileJSONInPkg(c.Extern))
213223
if err != nil {
214224
return nil, err

0 commit comments

Comments
 (0)