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

expression: should not pass nil pointer to HashCode() function #9324

Merged

Conversation

Projects
None yet
7 participants
@zz-jason
Copy link
Member

zz-jason commented Feb 16, 2019

What problem does this PR solve?

fix nil pointer deference in codec

Before this commit, the following SQL will panic:

drop table if exists t;
create table t(a timestamp) partition by range(unix_timestamp(a)) (partition p0 values less than(unix_timestamp('2019-02-19 14:20:00')), partition p1 values less than (maxvalue));
explain select * from t where a between timestamp'2019-02-19 14:19:00' and timestamp'2019-02-19 14:21:00';

error msg:

2019/02/19 12:37:17.005 conn.go:536: [error] lastCmd explain select * from t where a between timestamp'2019-02-19 14:19:00' and timestamp'2019-02-19 14:21:00', runtime error: invalid memory address or nil pointer dereference, goroutine 296 [running]:
github.com/pingcap/tidb/server.(*clientConn).Run.func1(0xc0005ba340)
        /Users/jianzhang.zj/opt/gopath/src/github.com/pingcap/tidb/server/conn.go:534 +0x10a
panic(0x4ff92a0, 0x6232ff0)
        /Users/jianzhang.zj/opt/goroot/src/runtime/panic.go:513 +0x1b9
github.com/pingcap/tidb/util/codec.encode(0x0, 0xc006d61078, 0x2, 0x8, 0xc008542ef0, 0x1, 0x1, 0x0, 0x8, 0x0, ...)
        /Users/jianzhang.zj/opt/gopath/src/github.com/pingcap/tidb/util/codec/codec.go:73 +0xb63
github.com/pingcap/tidb/util/codec.EncodeValue(0x0, 0xc006d61078, 0x1, 0x8, 0xc008542ef0, 0x1, 0x1, 0x8, 0x51926a0, 0xc008469050, ...)
        /Users/jianzhang.zj/opt/gopath/src/github.com/pingcap/tidb/util/codec/codec.go:180 +0x84
github.com/pingcap/tidb/expression.(*Constant).HashCode(0xc0085b04d0, 0x0, 0x9, 0x9, 0x9)
        /Users/jianzhang.zj/opt/gopath/src/github.com/pingcap/tidb/expression/constant.go:327 +0x138
github.com/pingcap/tidb/expression.(*ScalarFunction).HashCode(0xc0085091d0, 0x0, 0xc00859e5f0, 0x49, 0xc00858cfb8)
        /Users/jianzhang.zj/opt/gopath/src/github.com/pingcap/tidb/expression/scalar_function.go:256 +0x1a3
github.com/pingcap/tidb/expression.(*exprSet).Append(0xc0085aac40, 0x53fef60, 0xc0085091d0, 0xc008507e01)
        /Users/jianzhang.zj/opt/gopath/src/github.com/pingcap/tidb/expression/constraint_propagation.go:38 +0x41
github.com/pingcap/tidb/expression.newExprSet(0xc0085aab00, 0x3, 0x4, 0xc0085aab00)
        /Users/jianzhang.zj/opt/gopath/src/github.com/pingcap/tidb/expression/constraint_propagation.go:77 +0x15c
github.com/pingcap/tidb/expression.constraintSolver.Solve(0xc00000e4d0, 0x1, 0x1, 0x53fe720, 0xc0006ae540, 0xc0085aab00, 0x3, 0x4, 0x6d40000, 0x0, ...)
        /Users/jianzhang.zj/opt/gopath/src/github.com/pingcap/tidb/expression/constraint_propagation.go:97 +0x46
github.com/pingcap/tidb/planner/core.(*partitionProcessor).canBePruned(0x62aaa80, 0x53fe720, 0xc0006ae540, 0x0, 0x53fef60, 0xc0084434a0, 0xc0084fbf60, 0x2, 0x2, 0xc0085be6c0, ...)
        /Users/jianzhang.zj/opt/gopath/src/github.com/pingcap/tidb/planner/core/rule_partition_processor.go:142 +0x1b4
github.com/pingcap/tidb/planner/core.(*partitionProcessor).prune(0x62aaa80, 0xc0084dbe00, 0x0, 0xc00858cea0, 0x53ff860, 0xc00859d180)
        /Users/jianzhang.zj/opt/gopath/src/github.com/pingcap/tidb/planner/core/rule_partition_processor.go:91 +0x21b
github.com/pingcap/tidb/planner/core.(*partitionProcessor).rewriteDataSource(0x62aaa80, 0x53ff260, 0xc0084dbe00, 0x1, 0xc00859d180, 0x1, 0x0)
        /Users/jianzhang.zj/opt/gopath/src/github.com/pingcap/tidb/planner/core/rule_partition_processor.go:49 +0x58
github.com/pingcap/tidb/planner/core.(*partitionProcessor).rewriteDataSource(0x62aaa80, 0x53ff7a0, 0xc00858cea0, 0x0, 0x0, 0x53ff7a0, 0xc00858cea0)
        /Users/jianzhang.zj/opt/gopath/src/github.com/pingcap/tidb/planner/core/rule_partition_processor.go:53 +0xfe
github.com/pingcap/tidb/planner/core.(*partitionProcessor).optimize(0x62aaa80, 0x53ff7a0, 0xc00858cea0, 0x53ff7a0, 0xc00858cea0, 0x0, 0x0)
        /Users/jianzhang.zj/opt/gopath/src/github.com/pingcap/tidb/planner/core/rule_partition_processor.go:42 +0x3f
github.com/pingcap/tidb/planner/core.logicalOptimize(0x151, 0x53ff7a0, 0xc00858cea0, 0x511e8c0, 0x51bc740, 0x1, 0x53ff7a0)
        /Users/jianzhang.zj/opt/gopath/src/github.com/pingcap/tidb/planner/core/optimizer.go:130 +0xf6
github.com/pingcap/tidb/planner/core.DoOptimize(0x151, 0x53ff7a0, 0xc00858cea0, 0x53ff7a0, 0xc00858cea0, 0x1, 0x0)
        /Users/jianzhang.zj/opt/gopath/src/github.com/pingcap/tidb/planner/core/optimizer.go:100 +0x43
github.com/pingcap/tidb/planner.Optimize(0x53fe720, 0xc0006ae540, 0x53d8240, 0xc0006e4d80, 0x53fc720, 0xc0084e4c30, 0xc008543690, 0x400e74f, 0xc0006e4e40, 0xc0)
        /Users/jianzhang.zj/opt/gopath/src/github.com/pingcap/tidb/planner/optimize.go:68 +0x2ec
github.com/pingcap/tidb/planner/core.(*PlanBuilder).buildExplain(0xc0006e4e40, 0xc0085aa680, 0xc0085436a0, 0x400efd8, 0x30, 0x5174220)
        /Users/jianzhang.zj/opt/gopath/src/github.com/pingcap/tidb/planner/core/planbuilder.go:1613 +0xf1
github.com/pingcap/tidb/planner/core.(*PlanBuilder).Build(0xc0006e4e40, 0x53d7840, 0xc0085aa680, 0xc0085aa680, 0x0, 0x0, 0
2019/02/19 12:37:17.006 server.go:397: [info] con:3 close connection

What is changed and how it works?

do not pass a nil pointer to the HashCode() function

Check List

Tests

  • Integration test

Code changes

  • Has exported function/method change

Related changes

  • Need to cherry-pick to the release branch
@codecov-io

This comment has been minimized.

Copy link

codecov-io commented Feb 16, 2019

Codecov Report

Merging #9324 into master will increase coverage by 0.01%.
The diff coverage is 87.5%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #9324      +/-   ##
==========================================
+ Coverage   67.17%   67.19%   +0.01%     
==========================================
  Files         373      373              
  Lines       78271    78272       +1     
==========================================
+ Hits        52577    52591      +14     
+ Misses      20990    20981       -9     
+ Partials     4704     4700       -4
Impacted Files Coverage Δ
expression/constraint_propagation.go 73.61% <87.5%> (+0.16%) ⬆️
store/tikv/lock_resolver.go 41.7% <0%> (-0.95%) ⬇️
executor/join.go 78.9% <0%> (-0.53%) ⬇️
util/filesort/filesort.go 76.48% <0%> (+0.31%) ⬆️
store/tikv/2pc.go 80.4% <0%> (+1.78%) ⬆️
ddl/delete_range.go 79.36% <0%> (+4.23%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 67b5eef...b28c1cf. Read the comment docs.

@zz-jason

This comment has been minimized.

Copy link
Member Author

zz-jason commented Feb 16, 2019

/run-all-tests

@eurekaka
Copy link
Contributor

eurekaka left a comment

LGTM

@@ -34,14 +35,14 @@ type exprSet struct {
constfalse bool
}

func (s *exprSet) Append(e Expression) bool {
if _, ok := s.exists[string(e.HashCode(nil))]; ok {
func (s *exprSet) Append(sc *stmtctx.StatementContext, e Expression) bool {

This comment has been minimized.

Copy link
@tiancaiamao

tiancaiamao Feb 18, 2019

Contributor

This StatementContext maybe not the same one with the original StatementContext which is used in the create table DDL.
But I haven't find a better way to handle it yet.

zz-jason added some commits Feb 19, 2019

@zz-jason

This comment has been minimized.

Copy link
Member Author

zz-jason commented Feb 19, 2019

/run-all-tests

zz-jason added some commits Feb 19, 2019

@zz-jason zz-jason requested a review from tiancaiamao Feb 20, 2019

@tiancaiamao

This comment has been minimized.

Copy link
Contributor

tiancaiamao commented Feb 20, 2019

@tiancaiamao tiancaiamao added status/LGT2 and removed status/LGT1 labels Feb 20, 2019

@zz-jason

This comment has been minimized.

Copy link
Member Author

zz-jason commented Feb 20, 2019

/run-all-tests

@lamxTyler
Copy link
Member

lamxTyler left a comment

LGTM

lamxTyler and others added some commits Feb 21, 2019

@zz-jason zz-jason merged commit 38a453d into pingcap:master Feb 21, 2019

5 checks passed

ci/circleci Your tests passed on CircleCI!
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
idc-jenkins-ci-tidb/build Jenkins job succeeded.
Details
idc-jenkins-ci-tidb/check_dev Jenkins job succeeded.
Details
license/cla Contributor License Agreement is signed.
Details

@zz-jason zz-jason deleted the zz-jason:pquery/constraint-propagation-nil-pointer branch Feb 21, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.