Skip to content

Commit 92559a8

Browse files
committed
fixed staticcheck errors
1 parent 3df9532 commit 92559a8

22 files changed

+217
-248
lines changed

bad_server_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ func testBadServer(t *testing.T, handler func(net.Conn)) {
3636
go func() {
3737
conn, err := listener.Accept()
3838
if err != nil {
39-
t.Fatal("Failed to accept connection", err)
39+
t.Log("Failed to accept connection", err)
40+
return
4041
}
4142
handler(conn)
4243
_ = conn.Close()

buf.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ func (r *tdsBuffer) readNextPacket() error {
144144
return err
145145
}
146146
if int(h.Size) > r.packetSize {
147-
return errors.New("Invalid packet size, it is longer than buffer size")
147+
return errors.New("invalid packet size, it is longer than buffer size")
148148
}
149149
if headerSize > int(h.Size) {
150-
return errors.New("Invalid packet size, it is shorter than header size")
150+
return errors.New("invalid packet size, it is shorter than header size")
151151
}
152152
_, err = io.ReadFull(r.transport, r.rbuf[headerSize:h.Size])
153153
if err != nil {

buf_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestInvalidLengthInHeaderTooLong(t *testing.T) {
5353
if err == nil {
5454
t.Fatal("BeginRead was expected to return error but it didn't")
5555
} else {
56-
if err.Error() != "Invalid packet size, it is longer than buffer size" {
56+
if err.Error() != "invalid packet size, it is longer than buffer size" {
5757
t.Fatal("BeginRead failed with incorrect error", err)
5858
} else {
5959
t.Log("BeginRead failed as expected with error:", err.Error())
@@ -205,7 +205,7 @@ func TestWrite(t *testing.T) {
205205
if err != nil {
206206
t.Fatal("FinishPacket failed:", err.Error())
207207
}
208-
if bytes.Compare(memBuf.Bytes(), []byte{1, 1, 0, 11, 0, 0, 1, 0, 2, 3, 4}) != 0 {
208+
if !bytes.Equal(memBuf.Bytes(), []byte{1, 1, 0, 11, 0, 0, 1, 0, 2, 3, 4}) {
209209
t.Fatalf("Written buffer has invalid content: %v", memBuf.Bytes())
210210
}
211211

@@ -226,7 +226,7 @@ func TestWrite(t *testing.T) {
226226
2, 0, 0, 11, 0, 0, 1, 0, 3, 4, 5, // packet 2
227227
2, 1, 0, 9, 0, 0, 2, 0, 6, // packet 3
228228
}
229-
if bytes.Compare(memBuf.Bytes(), expectedBuf) != 0 {
229+
if !bytes.Equal(memBuf.Bytes(), expectedBuf) {
230230
t.Fatalf("Written buffer has invalid content:\n got: %v\nwant: %v", memBuf.Bytes(), expectedBuf)
231231
}
232232
}
@@ -295,7 +295,7 @@ func TestReadUsVarCharOrPanic(t *testing.T) {
295295
recover()
296296
}()
297297
memBuf = bytes.NewBuffer([]byte{})
298-
s = readUsVarCharOrPanic(memBuf)
298+
_ = readUsVarCharOrPanic(memBuf)
299299
t.Fatal("UsVarChar() should panic, but it didn't")
300300
}
301301

@@ -311,6 +311,6 @@ func TestReadBVarCharOrPanic(t *testing.T) {
311311
recover()
312312
}()
313313
memBuf = bytes.NewBuffer([]byte{})
314-
s = readBVarCharOrPanic(memBuf)
314+
_ = readBVarCharOrPanic(memBuf)
315315
t.Fatal("readBVarCharOrPanic() should panic on empty buffer, but it didn't")
316316
}

bulkcopy.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (b *Bulk) sendBulkCommand(ctx context.Context) (err error) {
8787
b.bulkColumns = append(b.bulkColumns, *bulkCol)
8888
b.dlogf("Adding column %s %s %#x", colname, bulkCol.ColName, bulkCol.ti.TypeId)
8989
} else {
90-
return fmt.Errorf("Column %s does not exist in destination table %s", colname, b.tablename)
90+
return fmt.Errorf("column %s does not exist in destination table %s", colname, b.tablename)
9191
}
9292
}
9393

@@ -167,7 +167,7 @@ func (b *Bulk) AddRow(row []interface{}) (err error) {
167167
}
168168

169169
if len(row) != len(b.bulkColumns) {
170-
return fmt.Errorf("Row does not have the same number of columns than the destination table %d %d",
170+
return fmt.Errorf("row does not have the same number of columns than the destination table %d %d",
171171
len(row), len(b.bulkColumns))
172172
}
173173

@@ -216,7 +216,7 @@ func (b *Bulk) makeRowData(row []interface{}) ([]byte, error) {
216216
}
217217

218218
func (b *Bulk) Done() (rowcount int64, err error) {
219-
if b.headerSent == false {
219+
if !b.headerSent {
220220
//no rows had been sent
221221
return 0, nil
222222
}

bulkcopy_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ func TestBulkcopy(t *testing.T) {
141141
//check that all rows are present
142142
var rowCount int
143143
err = conn.QueryRowContext(ctx, "select count(*) c from "+tableName).Scan(&rowCount)
144+
if err != nil {
145+
t.Fatal(err)
146+
}
144147

145148
if rowCount != 10 {
146149
t.Errorf("unexpected row count %d", rowCount)
@@ -156,7 +159,7 @@ func TestBulkcopy(t *testing.T) {
156159

157160
ptrs := make([]interface{}, len(columns))
158161
container := make([]interface{}, len(columns))
159-
for i, _ := range ptrs {
162+
for i := range ptrs {
160163
ptrs[i] = &container[i]
161164
}
162165
if err := rows.Scan(ptrs...); err != nil {

conn_str.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func parseConnectParams(dsn string) (connectParams, error) {
6868
var err error
6969
p.logFlags, err = strconv.ParseUint(strlog, 10, 64)
7070
if err != nil {
71-
return p, fmt.Errorf("Invalid log parameter '%s': %s", strlog, err.Error())
71+
return p, fmt.Errorf("invalid log parameter '%s': %s", strlog, err.Error())
7272
}
7373
}
7474
server := params["server"]
@@ -90,7 +90,7 @@ func parseConnectParams(dsn string) (connectParams, error) {
9090
var err error
9191
p.port, err = strconv.ParseUint(strport, 10, 16)
9292
if err != nil {
93-
f := "Invalid tcp port '%v': %v"
93+
f := "invalid tcp port '%v': %v"
9494
return p, fmt.Errorf(f, strport, err.Error())
9595
}
9696
}
@@ -102,7 +102,7 @@ func parseConnectParams(dsn string) (connectParams, error) {
102102
var err error
103103
psize, err := strconv.ParseUint(strpsize, 0, 16)
104104
if err != nil {
105-
f := "Invalid packet size '%v': %v"
105+
f := "invalid packet size '%v': %v"
106106
return p, fmt.Errorf(f, strpsize, err.Error())
107107
}
108108

@@ -125,7 +125,7 @@ func parseConnectParams(dsn string) (connectParams, error) {
125125
if strconntimeout, ok := params["connection timeout"]; ok {
126126
timeout, err := strconv.ParseUint(strconntimeout, 10, 64)
127127
if err != nil {
128-
f := "Invalid connection timeout '%v': %v"
128+
f := "invalid connection timeout '%v': %v"
129129
return p, fmt.Errorf(f, strconntimeout, err.Error())
130130
}
131131
p.conn_timeout = time.Duration(timeout) * time.Second
@@ -134,7 +134,7 @@ func parseConnectParams(dsn string) (connectParams, error) {
134134
if strdialtimeout, ok := params["dial timeout"]; ok {
135135
timeout, err := strconv.ParseUint(strdialtimeout, 10, 64)
136136
if err != nil {
137-
f := "Invalid dial timeout '%v': %v"
137+
f := "invalid dial timeout '%v': %v"
138138
return p, fmt.Errorf(f, strdialtimeout, err.Error())
139139
}
140140
p.dial_timeout = time.Duration(timeout) * time.Second
@@ -146,7 +146,7 @@ func parseConnectParams(dsn string) (connectParams, error) {
146146
if keepAlive, ok := params["keepalive"]; ok {
147147
timeout, err := strconv.ParseUint(keepAlive, 10, 64)
148148
if err != nil {
149-
f := "Invalid keepAlive value '%s': %s"
149+
f := "invalid keepAlive value '%s': %s"
150150
return p, fmt.Errorf(f, keepAlive, err.Error())
151151
}
152152
p.keepAlive = time.Duration(timeout) * time.Second
@@ -159,7 +159,7 @@ func parseConnectParams(dsn string) (connectParams, error) {
159159
var err error
160160
p.encrypt, err = strconv.ParseBool(encrypt)
161161
if err != nil {
162-
f := "Invalid encrypt '%s': %s"
162+
f := "invalid encrypt '%s': %s"
163163
return p, fmt.Errorf(f, encrypt, err.Error())
164164
}
165165
}
@@ -171,7 +171,7 @@ func parseConnectParams(dsn string) (connectParams, error) {
171171
var err error
172172
p.trustServerCertificate, err = strconv.ParseBool(trust)
173173
if err != nil {
174-
f := "Invalid trust server certificate '%s': %s"
174+
f := "invalid trust server certificate '%s': %s"
175175
return p, fmt.Errorf(f, trust, err.Error())
176176
}
177177
}
@@ -211,7 +211,7 @@ func parseConnectParams(dsn string) (connectParams, error) {
211211
if ok {
212212
if appintent == "ReadOnly" {
213213
if p.database == "" {
214-
return p, fmt.Errorf("Database must be specified when ApplicationIntent is ReadOnly")
214+
return p, fmt.Errorf("database must be specified when ApplicationIntent is ReadOnly")
215215
}
216216
p.typeFlags |= fReadOnlyIntent
217217
}
@@ -227,7 +227,7 @@ func parseConnectParams(dsn string) (connectParams, error) {
227227
var err error
228228
p.failOverPort, err = strconv.ParseUint(failOverPort, 0, 16)
229229
if err != nil {
230-
f := "Invalid tcp port '%v': %v"
230+
f := "invalid tcp port '%v': %v"
231231
return p, fmt.Errorf(f, failOverPort, err.Error())
232232
}
233233
}
@@ -366,7 +366,7 @@ func splitConnectionStringOdbc(dsn string) (map[string]string, error) {
366366
case parserStateBeforeKey:
367367
switch {
368368
case c == '=':
369-
return res, fmt.Errorf("Unexpected character = at index %d. Expected start of key or semi-colon or whitespace.", i)
369+
return res, fmt.Errorf("unexpected character = at index %d. Expected start of key or semi-colon or whitespace", i)
370370
case !unicode.IsSpace(c) && c != ';':
371371
state = parserStateKey
372372
key += string(c)
@@ -445,7 +445,7 @@ func splitConnectionStringOdbc(dsn string) (map[string]string, error) {
445445
case unicode.IsSpace(c):
446446
// Ignore whitespace
447447
default:
448-
return res, fmt.Errorf("Unexpected character %c at index %d. Expected semi-colon or whitespace.", c, i)
448+
return res, fmt.Errorf("unexpected character %c at index %d. Expected semi-colon or whitespace", c, i)
449449
}
450450

451451
case parserStateEndValue:
@@ -455,7 +455,7 @@ func splitConnectionStringOdbc(dsn string) (map[string]string, error) {
455455
case unicode.IsSpace(c):
456456
// Ignore whitespace
457457
default:
458-
return res, fmt.Errorf("Unexpected character %c at index %d. Expected semi-colon or whitespace.", c, i)
458+
return res, fmt.Errorf("unexpected character %c at index %d. Expected semi-colon or whitespace", c, i)
459459
}
460460
}
461461
}
@@ -470,7 +470,7 @@ func splitConnectionStringOdbc(dsn string) (map[string]string, error) {
470470
case parserStateBareValue:
471471
res[key] = strings.TrimRightFunc(value, unicode.IsSpace)
472472
case parserStateBracedValue:
473-
return res, fmt.Errorf("Unexpected end of braced value at index %d.", len(dsn))
473+
return res, fmt.Errorf("unexpected end of braced value at index %d", len(dsn))
474474
case parserStateBracedValueClosingBrace: // End of braced value
475475
res[key] = value
476476
case parserStateEndValue: // Okay

mssql.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ func (c *Conn) sendCommitRequest() error {
243243
c.sess.log.Printf("Failed to send CommitXact with %v", err)
244244
}
245245
c.connectionGood = false
246-
return fmt.Errorf("Faild to send CommitXact: %v", err)
246+
return fmt.Errorf("faild to send CommitXact: %v", err)
247247
}
248248
return nil
249249
}
@@ -270,7 +270,7 @@ func (c *Conn) sendRollbackRequest() error {
270270
c.sess.log.Printf("Failed to send RollbackXact with %v", err)
271271
}
272272
c.connectionGood = false
273-
return fmt.Errorf("Failed to send RollbackXact: %v", err)
273+
return fmt.Errorf("failed to send RollbackXact: %v", err)
274274
}
275275
return nil
276276
}
@@ -307,7 +307,7 @@ func (c *Conn) sendBeginRequest(ctx context.Context, tdsIsolation isoLevel) erro
307307
c.sess.log.Printf("Failed to send BeginXact with %v", err)
308308
}
309309
c.connectionGood = false
310-
return fmt.Errorf("Failed to send BeginXact: %v", err)
310+
return fmt.Errorf("failed to send BeginXact: %v", err)
311311
}
312312
return nil
313313
}
@@ -482,7 +482,7 @@ func (s *Stmt) sendQuery(args []namedValue) (err error) {
482482
conn.sess.log.Printf("Failed to send Rpc with %v", err)
483483
}
484484
conn.connectionGood = false
485-
return fmt.Errorf("Failed to send RPC: %v", err)
485+
return fmt.Errorf("failed to send RPC: %v", err)
486486
}
487487
}
488488
return
@@ -923,7 +923,7 @@ func (c *Conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, e
923923
return nil, driver.ErrBadConn
924924
}
925925
if opts.ReadOnly {
926-
return nil, errors.New("Read-only transactions are not supported")
926+
return nil, errors.New("read-only transactions are not supported")
927927
}
928928

929929
var tdsIsolation isoLevel
@@ -945,7 +945,7 @@ func (c *Conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, e
945945
case sql.LevelLinearizable:
946946
return nil, errors.New("LevelLinearizable isolation level is not supported")
947947
default:
948-
return nil, errors.New("Isolation level is not supported or unknown")
948+
return nil, errors.New("isolation level is not supported or unknown")
949949
}
950950
return c.begin(ctx, tdsIsolation)
951951
}

mssql_go110.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,5 @@ func (c *Connector) Driver() driver.Driver {
4848
}
4949

5050
func (r *Result) LastInsertId() (int64, error) {
51-
return -1, errors.New("LastInsertId is not supported. Please use the OUTPUT clause or add `select ID = convert(bigint, SCOPE_IDENTITY())` to the end of your query.")
51+
return -1, errors.New("LastInsertId is not supported. Please use the OUTPUT clause or add `select ID = convert(bigint, SCOPE_IDENTITY())` to the end of your query")
5252
}

net.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (c *tlsHandshakeConn) Read(b []byte) (n int, err error) {
7575
c.packetPending = false
7676
err = c.buf.FinishPacket()
7777
if err != nil {
78-
err = fmt.Errorf("Cannot send handshake packet: %s", err.Error())
78+
err = fmt.Errorf("cannot send handshake packet: %s", err.Error())
7979
return
8080
}
8181
c.continueRead = false
@@ -84,7 +84,7 @@ func (c *tlsHandshakeConn) Read(b []byte) (n int, err error) {
8484
var packet packetType
8585
packet, err = c.buf.BeginRead()
8686
if err != nil {
87-
err = fmt.Errorf("Cannot read handshake packet: %s", err.Error())
87+
err = fmt.Errorf("cannot read handshake packet: %s", err.Error())
8888
return
8989
}
9090
if packet != packPrelogin {

ntlm.go

+1-12
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"time"
1515
"unicode/utf16"
1616

17+
//lint:ignore SA1019 MD4 is used by legacy NTLM
1718
"golang.org/x/crypto/md4"
1819
)
1920

@@ -126,18 +127,6 @@ func createDesKey(bytes, material []byte) {
126127
material[7] = (byte)(bytes[6] << 1)
127128
}
128129

129-
func oddParity(bytes []byte) {
130-
for i := 0; i < len(bytes); i++ {
131-
b := bytes[i]
132-
needsParity := (((b >> 7) ^ (b >> 6) ^ (b >> 5) ^ (b >> 4) ^ (b >> 3) ^ (b >> 2) ^ (b >> 1)) & 0x01) == 0
133-
if needsParity {
134-
bytes[i] = bytes[i] | byte(0x01)
135-
} else {
136-
bytes[i] = bytes[i] & byte(0xfe)
137-
}
138-
}
139-
}
140-
141130
func encryptDes(key []byte, cleartext []byte, ciphertext []byte) {
142131
var desKey [8]byte
143132
createDesKey(key, desKey[:])

ntlm_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ func TestNTLMV2Response(t *testing.T) {
9494
expectedNTLMV2Response, _ := hex.DecodeString("5c788afec59c1fef3f90bf6ea419c02501010000000000000fc4a4d5fdf6b200ffffff00112233440000000002000c0044004f004d00410049004e0001000c005300450052005600450052000400140064006f006d00610069006e002e0063006f006d00030022007300650072007600650072002e0064006f006d00610069006e002e0063006f006d000000000000000000")
9595
expectedLMV2Response, _ := hex.DecodeString("d6e6152ea25d03b7c6ba6629c2d6aaf0ffffff0011223344")
9696
ntlmV2Response, lmV2Response := getNTLMv2AndLMv2ResponsePayloads(target, username, password, challenge, nonce, targetInformationBlock, timestamp)
97-
if bytes.Compare(ntlmV2Response, expectedNTLMV2Response) != 0 {
97+
if !bytes.Equal(ntlmV2Response, expectedNTLMV2Response) {
9898
t.Errorf("got:\n%s\nexpected:\n%s", hex.Dump(ntlmV2Response), hex.Dump(expectedNTLMV2Response))
9999
}
100100

101-
if bytes.Compare(lmV2Response, expectedLMV2Response) != 0 {
101+
if !bytes.Equal(lmV2Response, expectedLMV2Response) {
102102
t.Errorf("got:\n%s\nexpected:\n%s", hex.Dump(ntlmV2Response), hex.Dump(expectedNTLMV2Response))
103103
}
104104
}

queries_go110_test.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ func TestReturnStatusWithQuery(t *testing.T) {
212212
var str string
213213
for rows.Next() {
214214
err = rows.Scan(&str)
215+
if err != nil {
216+
t.Fatal("scan failed", err)
217+
}
215218
if str != "value" {
216219
t.Errorf("expected str=value, got %s", str)
217220
}
@@ -231,17 +234,17 @@ func TestIdentity(t *testing.T) {
231234
}
232235
defer tx.Rollback()
233236

234-
res, err := tx.Exec("create table #foo (bar int identity, baz int unique)")
237+
_, err = tx.Exec("create table #foo (bar int identity, baz int unique)")
235238
if err != nil {
236239
t.Fatal("create table failed")
237240
}
238241

239-
res, err = tx.Exec("insert into #foo (baz) values (1)")
242+
res, err := tx.Exec("insert into #foo (baz) values (1)")
240243
if err != nil {
241244
t.Fatal("insert failed")
242245
}
243246
n, err := res.LastInsertId()
244-
expErr := "LastInsertId is not supported. Please use the OUTPUT clause or add `select ID = convert(bigint, SCOPE_IDENTITY())` to the end of your query."
247+
expErr := "LastInsertId is not supported. Please use the OUTPUT clause or add `select ID = convert(bigint, SCOPE_IDENTITY())` to the end of your query"
245248
if err == nil {
246249
t.Fatal("Expected an error from LastInsertId, didn't get an error")
247250
} else if err.Error() != expErr {

0 commit comments

Comments
 (0)