Skip to content

Commit f97ddc7

Browse files
authored
Merge pull request #1195 from korovindenis/master
enable forcetypeassert linter
2 parents 57d4654 + 5a238a1 commit f97ddc7

File tree

17 files changed

+213
-39
lines changed

17 files changed

+213
-39
lines changed

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ linters:
226226
- exhaustivestruct
227227
- exhaustruct
228228
- forbidigo
229-
- forcetypeassert
230229
- funlen
231230
- gochecknoglobals
232231
- gocognit
@@ -297,6 +296,7 @@ issues:
297296
- unused
298297
- unparam
299298
- gocritic
299+
- forcetypeassert
300300
- path: topic/topicreader/reader_example_test.go
301301
linters:
302302
- staticcheck

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* Added type assertion checks to enhance type safety and prevent unexpected panics in critical sections of the codebase
2+
13
## v3.66.3
24
* Fixed the OAuth2 test
35

internal/allocator/allocator.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package allocator
22

33
import (
4+
"fmt"
45
"sync"
56

67
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
@@ -1108,7 +1109,12 @@ func (p *Pool[T]) Get() *T {
11081109
v = &zero
11091110
}
11101111

1111-
return v.(*T)
1112+
val, ok := v.(*T)
1113+
if !ok {
1114+
panic(fmt.Sprintf("unsupported type conversion from %T to *T", val))
1115+
}
1116+
1117+
return val
11121118
}
11131119

11141120
func (p *Pool[T]) Put(t *T) {

internal/bind/numeric_args.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ func (m NumericArgs) RewriteQuery(sql string, args ...interface{}) (yql string,
5656
)
5757
}
5858
paramIndex := int(p - 1)
59-
buffer.WriteString(newArgs[paramIndex].(table.ParameterOption).Name())
59+
val, ok := newArgs[paramIndex].(table.ParameterOption)
60+
if !ok {
61+
panic(fmt.Sprintf("unsupported type conversion from %T to table.ParameterOption", val))
62+
}
63+
buffer.WriteString(val.Name())
6064
}
6165
}
6266

internal/cmd/gtrace/writer.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ func (w *Writer) init() {
8686
}
8787

8888
func (w *Writer) mustDeclare(name string) {
89-
s := w.scope.Back().Value.(*scope)
89+
s, ok := w.scope.Back().Value.(*scope)
90+
if !ok {
91+
panic(fmt.Sprintf("unsupported type conversion from %T to *scope", s))
92+
}
9093
if !s.set(name) {
9194
where := s.where(name)
9295
panic(fmt.Sprintf(
@@ -100,7 +103,10 @@ func (w *Writer) declare(name string) string {
100103
if isPredeclared(name) {
101104
name = firstChar(name)
102105
}
103-
s := w.scope.Back().Value.(*scope)
106+
s, ok := w.scope.Back().Value.(*scope)
107+
if !ok {
108+
panic(fmt.Sprintf("unsupported type conversion from %T to *scope", s))
109+
}
104110
for i := 0; ; i++ {
105111
v := name
106112
if i > 0 {
@@ -127,7 +133,10 @@ func (w *Writer) isGlobalScope() bool {
127133
}
128134

129135
func (w *Writer) capture(vars ...string) {
130-
s := w.scope.Back().Value.(*scope)
136+
s, ok := w.scope.Back().Value.(*scope)
137+
if !ok {
138+
panic(fmt.Sprintf("unsupported type conversion from %T to *scope", s))
139+
}
131140
for _, v := range vars {
132141
if !s.set(v) {
133142
panic(fmt.Sprintf("can't capture variable %q", v))

internal/conn/conn.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,12 @@ func getContextMark(ctx context.Context) *modificationMark {
559559
return &modificationMark{}
560560
}
561561

562-
return v.(*modificationMark)
562+
val, ok := v.(*modificationMark)
563+
if !ok {
564+
panic(fmt.Sprintf("unsupported type conversion from %T to *modificationMark", val))
565+
}
566+
567+
return val
563568
}
564569

565570
type modificationMark struct {

internal/table/client.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -615,12 +615,18 @@ func (c *Client) Close(ctx context.Context) (err error) {
615615
c.limit = 0
616616

617617
for el := c.waitQ.Front(); el != nil; el = el.Next() {
618-
ch := el.Value.(*chan *session)
618+
ch, ok := el.Value.(*chan *session)
619+
if !ok {
620+
panic(fmt.Sprintf("unsupported type conversion from %T to *chan *session", ch))
621+
}
619622
close(*ch)
620623
}
621624

622625
for e := c.idle.Front(); e != nil; e = e.Next() {
623-
s := e.Value.(*session)
626+
s, ok := e.Value.(*session)
627+
if !ok {
628+
panic(fmt.Sprintf("unsupported type conversion from %T to *session", s))
629+
}
624630
s.SetStatus(table.SessionClosing)
625631
c.wg.Add(1)
626632
go func() {
@@ -745,7 +751,10 @@ func (c *Client) internalPoolGCTick(ctx context.Context, idleThreshold time.Dura
745751
return
746752
}
747753
for e := c.idle.Front(); e != nil; e = e.Next() {
748-
s := e.Value.(*session)
754+
s, ok := e.Value.(*session)
755+
if !ok {
756+
panic(fmt.Sprintf("unsupported type conversion from %T to *session", s))
757+
}
749758
info, has := c.index[s]
750759
if !has {
751760
panic("session not found in pool")
@@ -818,7 +827,10 @@ func (c *Client) internalPoolPeekFirstIdle() (s *session, touched time.Time) {
818827
if el == nil {
819828
return
820829
}
821-
s = el.Value.(*session)
830+
s, ok := el.Value.(*session)
831+
if !ok {
832+
panic(fmt.Sprintf("unsupported type conversion from %T to *session", s))
833+
}
822834
info, has := c.index[s]
823835
if !has || el != info.idle {
824836
panic("inconsistent session client index")
@@ -857,7 +869,10 @@ func (c *Client) internalPoolNotify(s *session) (notified bool) {
857869
// missed something and may want to retry (especially for case (3)).
858870
//
859871
// After that we taking a next waiter and repeat the same.
860-
ch := c.waitQ.Remove(el).(*chan *session)
872+
ch, ok := c.waitQ.Remove(el).(*chan *session)
873+
if !ok {
874+
panic(fmt.Sprintf("unsupported type conversion from %T to *chan *session", ch))
875+
}
861876
select {
862877
case *ch <- s:
863878
// Case (1).

internal/table/scanner/scan_raw.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package scanner
22

33
import (
44
"bytes"
5+
"fmt"
56
"io"
67
"reflect"
78
"strconv"
@@ -584,7 +585,10 @@ func (s *rawConverter) IsDecimal() bool {
584585
}
585586

586587
func isEqualDecimal(d *Ydb.DecimalType, t types.Type) bool {
587-
w := t.(*types.Decimal)
588+
w, ok := t.(*types.Decimal)
589+
if !ok {
590+
panic(fmt.Sprintf("unsupported type conversion from %T to *types.Decimal", w))
591+
}
588592

589593
return d.GetPrecision() == w.Precision() && d.GetScale() == w.Scale()
590594
}

internal/table/transaction.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,15 @@ func (tx *transaction) ExecuteStatement(
9898
a := allocator.New()
9999
defer a.Free()
100100

101+
val, ok := stmt.(*statement)
102+
if !ok {
103+
panic(fmt.Sprintf("unsupported type conversion from %T to *statement", val))
104+
}
105+
101106
onDone := trace.TableOnTxExecuteStatement(
102107
tx.s.config.Trace(), &ctx,
103108
stack.FunctionID("github.com/ydb-platform/ydb-go-sdk/3/internal/table.(*transaction).ExecuteStatement"),
104-
tx.s, tx, stmt.(*statement).query, parameters,
109+
tx.s, tx, val.query, parameters,
105110
)
106111
defer func() {
107112
onDone(r, err)

internal/types/types.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,12 @@ func (v *VariantStruct) ToYDB(a *allocator.Allocator) *Ydb.Type {
824824
typeVariant.VariantType = a.Variant()
825825

826826
structItems := a.VariantStructItems()
827-
structItems.StructItems = v.Struct.ToYDB(a).GetType().(*Ydb.Type_StructType).StructType
827+
828+
val, ok := v.Struct.ToYDB(a).GetType().(*Ydb.Type_StructType)
829+
if !ok {
830+
panic(fmt.Sprintf("unsupported type conversion from %T to *Ydb.Type_StructType", val))
831+
}
832+
structItems.StructItems = val.StructType
828833

829834
typeVariant.VariantType.Type = structItems
830835

@@ -877,7 +882,12 @@ func (v *VariantTuple) ToYDB(a *allocator.Allocator) *Ydb.Type {
877882
typeVariant.VariantType = a.Variant()
878883

879884
tupleItems := a.VariantTupleItems()
880-
tupleItems.TupleItems = v.Tuple.ToYDB(a).GetType().(*Ydb.Type_TupleType).TupleType
885+
886+
val, ok := v.Tuple.ToYDB(a).GetType().(*Ydb.Type_TupleType)
887+
if !ok {
888+
panic(fmt.Sprintf("unsupported type conversion from %T to *Ydb.Type_TupleType", val))
889+
}
890+
tupleItems.TupleItems = val.TupleType
881891

882892
typeVariant.VariantType.Type = tupleItems
883893

0 commit comments

Comments
 (0)