Skip to content

[BUG] - false positive errors on verify types of paramters #798

Open
@c14h3

Description

@c14h3

gocron/scheduler.go

Lines 577 to 616 in 31de1e8

func (s *scheduler) verifyVariadic(taskFunc reflect.Value, tsk task, variadicStart int) error {
if err := s.verifyNonVariadic(taskFunc, tsk, variadicStart); err != nil {
return err
}
parameterType := taskFunc.Type().In(variadicStart).Elem().Kind()
if parameterType == reflect.Interface {
return s.verifyInterfaceVariadic(taskFunc, tsk, variadicStart)
}
if parameterType == reflect.Pointer {
parameterType = reflect.Indirect(reflect.ValueOf(taskFunc.Type().In(variadicStart))).Kind()
}
for i := variadicStart; i < len(tsk.parameters); i++ {
argumentType := reflect.TypeOf(tsk.parameters[i]).Kind()
if argumentType == reflect.Interface || argumentType == reflect.Pointer {
argumentType = reflect.TypeOf(tsk.parameters[i]).Elem().Kind()
}
if argumentType != parameterType {
return ErrNewJobWrongTypeOfParameters
}
}
return nil
}
func (s *scheduler) verifyNonVariadic(taskFunc reflect.Value, tsk task, length int) error {
for i := 0; i < length; i++ {
t1 := reflect.TypeOf(tsk.parameters[i]).Kind()
if t1 == reflect.Interface || t1 == reflect.Pointer {
t1 = reflect.TypeOf(tsk.parameters[i]).Elem().Kind()
}
t2 := reflect.New(taskFunc.Type().In(i)).Elem().Kind()
if t2 == reflect.Interface || t2 == reflect.Pointer {
t2 = reflect.Indirect(reflect.ValueOf(taskFunc.Type().In(i))).Kind()
}
if t1 != t2 {
return ErrNewJobWrongTypeOfParameters
}
}
return nil
}

Issue 1: providing a function like Foo(bar any) t2 will be of type interface and its indirect type is struct, so if t1 is not of type struct it will cause an error but bar can handle everything, which leads into false positive error
Issue 2: providing a function like Foo(bar **int) t2 will be of type pointer and its indirect type is also pointer, so t1 may not point to the correct type of t2, which leads into false positive no error
Issue 3: providing a function like Foo(bar struct{}) t2 will be of type struct but struct != struct, so t1 must match the syntax of t2 struct. just checking struct must be struct may lead into false positive no error

I first considered open a pull request, but solving the issues is not trivial, so I would prefer to just omit the verify check and let the implementation raise panics if devs define task values wrong.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions