Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BAAS-33539: Add nil check in the builtin_date.go #126

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion builtin_date.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,12 @@ func mkTime(year, m, day, hour, min, sec, nsec int64, loc *time.Location) (t tim
}

func _intArg(call FunctionCall, argNum int) (int64, bool) {
n := call.Argument(argNum).ToNumber()
arg := call.Argument(argNum)
if arg == nil {
return 0, false
}

n := arg.ToNumber()
if IsNaN(n) {
return 0, false
}
Expand Down
62 changes: 62 additions & 0 deletions builtin_date_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package goja

import (
"testing"
)

func TestIntArg(t *testing.T) {
r := New()
for _, tc := range []struct {
description string
argNum int
funcCall FunctionCall
expectedValue int64
expectedOk bool
}{
{
description: "should return time value properly",
argNum: 0,
funcCall: FunctionCall{ctx: r.vm.ctx, Arguments: []Value{valueInt(2024)}},
expectedValue: 2024,
expectedOk: true,
},
{
description: "should return 0 when one argument is nil",
argNum: 0,
funcCall: FunctionCall{ctx: r.vm.ctx, Arguments: []Value{nil}},
expectedValue: 0,
expectedOk: false,
},
{
description: "should return 0 when argument is empty",
argNum: 0,
funcCall: FunctionCall{ctx: r.vm.ctx, Arguments: []Value{}},
expectedValue: 0,
expectedOk: false,
},
{
description: "should return 0 when the args field is nil",
argNum: 0,
funcCall: FunctionCall{ctx: r.vm.ctx},
expectedValue: 0,
expectedOk: false,
},
} {
t.Run(tc.description, func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Fatalf("should not have panic: %v", r)
}
}()

res, ok := _intArg(tc.funcCall, tc.argNum)
if ok != tc.expectedOk {
t.Fatalf("Expected ok value: %t, actual ok value: %t", tc.expectedOk, ok)
}
if res != tc.expectedValue {
t.Fatalf("Expected time value: %d, actual time value: %d", tc.expectedValue, res)
}
})
}

}
Loading