Skip to content

Commit

Permalink
sql: require valid TenantID in start_replication_stream
Browse files Browse the repository at this point in the history
MakeTenantID panics if someone tries to use 0 as a tenant ID. Here, we
return an error immediately in this case rather than crashing.

Fixes #71311

Release note: None
  • Loading branch information
stevendanna committed Nov 25, 2021
1 parent 38fd406 commit d9cdce8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
40 changes: 24 additions & 16 deletions pkg/sql/sem/builtins/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@ func newEncodeError(c rune, enc string) error {
"character %q has no representation in encoding %q", c, enc)
}

func mustBeDIntInTenantRange(e tree.Expr) (tree.DInt, error) {
tenID := tree.MustBeDInt(e)
if int64(tenID) <= 0 {
return 0, pgerror.New(pgcode.InvalidParameterValue, "tenant ID must be positive")
}
return tenID, nil
}

// builtins contains the built-in functions indexed by name.
//
// For use in other packages, see AllBuiltinNames and GetBuiltinProperties().
Expand Down Expand Up @@ -4426,9 +4434,9 @@ value if you rely on the HLC for accuracy.`,
if err := requireNonNull(args[0]); err != nil {
return nil, err
}
sTenID := int64(tree.MustBeDInt(args[0]))
if sTenID <= 0 {
return nil, pgerror.New(pgcode.InvalidParameterValue, "tenant ID must be positive")
sTenID, err := mustBeDIntInTenantRange(args[0])
if err != nil {
return nil, err
}
if err := ctx.Tenant.CreateTenant(ctx.Context, uint64(sTenID)); err != nil {
return nil, err
Expand Down Expand Up @@ -4468,9 +4476,9 @@ value if you rely on the HLC for accuracy.`,
},
ReturnType: tree.FixedReturnType(types.Int),
Fn: func(ctx *tree.EvalContext, args tree.Datums) (tree.Datum, error) {
sTenID := int64(tree.MustBeDInt(args[0]))
if sTenID <= 0 {
return nil, pgerror.New(pgcode.InvalidParameterValue, "tenant ID must be positive")
sTenID, err := mustBeDIntInTenantRange(args[0])
if err != nil {
return nil, err
}
if err := ctx.Tenant.DestroyTenant(ctx.Context, uint64(sTenID)); err != nil {
return nil, err
Expand Down Expand Up @@ -5580,9 +5588,9 @@ value if you rely on the HLC for accuracy.`,
},
ReturnType: tree.FixedReturnType(types.Int),
Fn: func(ctx *tree.EvalContext, args tree.Datums) (tree.Datum, error) {
sTenID := int64(tree.MustBeDInt(args[0]))
if sTenID <= 0 {
return nil, pgerror.New(pgcode.InvalidParameterValue, "tenant ID must be positive")
sTenID, err := mustBeDIntInTenantRange(args[0])
if err != nil {
return nil, err
}
if err := ctx.Tenant.GCTenant(ctx.Context, uint64(sTenID)); err != nil {
return nil, err
Expand Down Expand Up @@ -5611,9 +5619,9 @@ value if you rely on the HLC for accuracy.`,
},
ReturnType: tree.FixedReturnType(types.Int),
Fn: func(ctx *tree.EvalContext, args tree.Datums) (tree.Datum, error) {
sTenID := int64(tree.MustBeDInt(args[0]))
if sTenID <= 0 {
return nil, pgerror.New(pgcode.InvalidParameterValue, "tenant ID must be positive")
sTenID, err := mustBeDIntInTenantRange(args[0])
if err != nil {
return nil, err
}
availableRU := float64(tree.MustBeDFloat(args[1]))
refillRate := float64(tree.MustBeDFloat(args[2]))
Expand Down Expand Up @@ -5869,8 +5877,8 @@ the locality flag on node startup. Returns an error if no region is set.`,
}
return tree.MakeDBool(true), nil
},
Info: `Resets the zone configuration for a multi-region table to
match its original state. No-ops if the given table ID is not a multi-region
Info: `Resets the zone configuration for a multi-region table to
match its original state. No-ops if the given table ID is not a multi-region
table.`,
Volatility: tree.VolatilityVolatile,
},
Expand All @@ -5891,8 +5899,8 @@ table.`,
}
return tree.MakeDBool(true), nil
},
Info: `Resets the zone configuration for a multi-region database to
match its original state. No-ops if the given database ID is not multi-region
Info: `Resets the zone configuration for a multi-region database to
match its original state. No-ops if the given database ID is not multi-region
enabled.`,
Volatility: tree.VolatilityVolatile,
},
Expand Down
6 changes: 4 additions & 2 deletions pkg/sql/sem/builtins/replication_builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ var replicationBuiltins = map[string]builtinDefinition{
if err != nil {
return nil, err
}

tenantID := int(tree.MustBeDInt(args[0]))
tenantID, err := mustBeDIntInTenantRange(args[0])
if err != nil {
return nil, err
}
jobID, err := mgr.StartReplicationStream(evalCtx, evalCtx.Txn, uint64(tenantID))
if err != nil {
return nil, err
Expand Down

0 comments on commit d9cdce8

Please sign in to comment.