Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .chloggen/pprofile-off-by-one.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: 'bug_fix'

# The name of the component, or a single word describing the area of concern, (e.g. receiver/otlp)
component: pdata/pprofile

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fix off-by-one issue in dictionary lookups.

# One or more tracking issues or pull requests related to the change
issues: [14534]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
6 changes: 3 additions & 3 deletions pdata/pprofile/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (fn Function) Equal(val Function) bool {
// dictionary to another.
func (fn Function) switchDictionary(src, dst ProfilesDictionary) error {
if fn.NameStrindex() > 0 {
if src.StringTable().Len() < int(fn.NameStrindex()) {
if src.StringTable().Len() <= int(fn.NameStrindex()) {
return fmt.Errorf("invalid name index %d", fn.NameStrindex())
}

Expand All @@ -29,7 +29,7 @@ func (fn Function) switchDictionary(src, dst ProfilesDictionary) error {
}

if fn.SystemNameStrindex() > 0 {
if src.StringTable().Len() < int(fn.SystemNameStrindex()) {
if src.StringTable().Len() <= int(fn.SystemNameStrindex()) {
return fmt.Errorf("invalid system name index %d", fn.SystemNameStrindex())
}

Expand All @@ -41,7 +41,7 @@ func (fn Function) switchDictionary(src, dst ProfilesDictionary) error {
}

if fn.FilenameStrindex() > 0 {
if src.StringTable().Len() < int(fn.FilenameStrindex()) {
if src.StringTable().Len() <= int(fn.FilenameStrindex()) {
return fmt.Errorf("invalid filename index %d", fn.FilenameStrindex())
}

Expand Down
69 changes: 69 additions & 0 deletions pdata/pprofile/function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,29 @@ func TestFunctionSwitchDictionary(t *testing.T) {
wantDictionary: NewProfilesDictionary(),
wantErr: errors.New("invalid name index 1"),
},
{
name: "with a name index equal to the source table length (boundary condition)",
function: func() Function {
fn := NewFunction()
fn.SetNameStrindex(2) // Index 2 with length 2 (indices 0,1 are valid)
return fn
}(),

src: func() ProfilesDictionary {
d := NewProfilesDictionary()
d.StringTable().Append("", "test") // Length 2: indices 0,1 valid
return d
}(),
dst: NewProfilesDictionary(),

wantFunction: func() Function {
fn := NewFunction()
fn.SetNameStrindex(2)
return fn
}(),
wantDictionary: NewProfilesDictionary(),
wantErr: errors.New("invalid name index 2"),
},
{
name: "with an existing system name",
function: func() Function {
Expand Down Expand Up @@ -187,6 +210,29 @@ func TestFunctionSwitchDictionary(t *testing.T) {
wantDictionary: NewProfilesDictionary(),
wantErr: errors.New("invalid system name index 1"),
},
{
name: "with a system name index equal to the source table length (boundary condition)",
function: func() Function {
fn := NewFunction()
fn.SetSystemNameStrindex(2)
return fn
}(),

src: func() ProfilesDictionary {
d := NewProfilesDictionary()
d.StringTable().Append("", "test")
return d
}(),
dst: NewProfilesDictionary(),

wantFunction: func() Function {
fn := NewFunction()
fn.SetSystemNameStrindex(2)
return fn
}(),
wantDictionary: NewProfilesDictionary(),
wantErr: errors.New("invalid system name index 2"),
},
{
name: "with an existing filename",
function: func() Function {
Expand Down Expand Up @@ -236,6 +282,29 @@ func TestFunctionSwitchDictionary(t *testing.T) {
wantDictionary: NewProfilesDictionary(),
wantErr: errors.New("invalid filename index 1"),
},
{
name: "with a filename index equal to the source table length (boundary condition)",
function: func() Function {
fn := NewFunction()
fn.SetFilenameStrindex(2)
return fn
}(),

src: func() ProfilesDictionary {
d := NewProfilesDictionary()
d.StringTable().Append("", "test")
return d
}(),
dst: NewProfilesDictionary(),

wantFunction: func() Function {
fn := NewFunction()
fn.SetFilenameStrindex(2)
return fn
}(),
wantDictionary: NewProfilesDictionary(),
wantErr: errors.New("invalid filename index 2"),
},
} {
t.Run(tt.name, func(t *testing.T) {
fn := tt.function
Expand Down
4 changes: 2 additions & 2 deletions pdata/pprofile/keyvalueandunit.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (ms KeyValueAndUnit) Equal(val KeyValueAndUnit) bool {
// dictionary to another.
func (ms KeyValueAndUnit) switchDictionary(src, dst ProfilesDictionary) error {
if ms.KeyStrindex() > 0 {
if src.StringTable().Len() < int(ms.KeyStrindex()) {
if src.StringTable().Len() <= int(ms.KeyStrindex()) {
return fmt.Errorf("invalid key index %d", ms.KeyStrindex())
}

Expand All @@ -29,7 +29,7 @@ func (ms KeyValueAndUnit) switchDictionary(src, dst ProfilesDictionary) error {
}

if ms.UnitStrindex() > 0 {
if src.StringTable().Len() < int(ms.UnitStrindex()) {
if src.StringTable().Len() <= int(ms.UnitStrindex()) {
return fmt.Errorf("invalid unit index %d", ms.UnitStrindex())
}

Expand Down
46 changes: 46 additions & 0 deletions pdata/pprofile/keyvalueandunit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,29 @@ func TestKeyValueAndUnitSwitchDictionary(t *testing.T) {
wantDictionary: NewProfilesDictionary(),
wantErr: errors.New("invalid key index 1"),
},
{
name: "with a key index equal to the source table length (boundary condition)",
keyValueAndUnit: func() KeyValueAndUnit {
kvu := NewKeyValueAndUnit()
kvu.SetKeyStrindex(2)
return kvu
}(),

src: func() ProfilesDictionary {
d := NewProfilesDictionary()
d.StringTable().Append("", "test")
return d
}(),
dst: NewProfilesDictionary(),

wantKeyValueAndUnit: func() KeyValueAndUnit {
kvu := NewKeyValueAndUnit()
kvu.SetKeyStrindex(2)
return kvu
}(),
wantDictionary: NewProfilesDictionary(),
wantErr: errors.New("invalid key index 2"),
},
{
name: "with an existing unit",
keyValueAndUnit: func() KeyValueAndUnit {
Expand Down Expand Up @@ -182,6 +205,29 @@ func TestKeyValueAndUnitSwitchDictionary(t *testing.T) {
wantDictionary: NewProfilesDictionary(),
wantErr: errors.New("invalid unit index 1"),
},
{
name: "with a unit index equal to the source table length (boundary condition)",
keyValueAndUnit: func() KeyValueAndUnit {
kvu := NewKeyValueAndUnit()
kvu.SetUnitStrindex(2)
return kvu
}(),

src: func() ProfilesDictionary {
d := NewProfilesDictionary()
d.StringTable().Append("", "test")
return d
}(),
dst: NewProfilesDictionary(),

wantKeyValueAndUnit: func() KeyValueAndUnit {
kvu := NewKeyValueAndUnit()
kvu.SetUnitStrindex(2)
return kvu
}(),
wantDictionary: NewProfilesDictionary(),
wantErr: errors.New("invalid unit index 2"),
},
} {
t.Run(tt.name, func(t *testing.T) {
kvu := tt.keyValueAndUnit
Expand Down
2 changes: 1 addition & 1 deletion pdata/pprofile/line.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (l Line) Equal(val Line) bool {
// dictionary to another.
func (l Line) switchDictionary(src, dst ProfilesDictionary) error {
if l.FunctionIndex() > 0 {
if src.FunctionTable().Len() < int(l.FunctionIndex()) {
if src.FunctionTable().Len() <= int(l.FunctionIndex()) {
return fmt.Errorf("invalid function index %d", l.FunctionIndex())
}

Expand Down
24 changes: 24 additions & 0 deletions pdata/pprofile/line_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,30 @@ func TestLineSwitchDictionary(t *testing.T) {
wantDictionary: NewProfilesDictionary(),
wantErr: errors.New("invalid function index 1"),
},
{
name: "with a function index equal to the source table length (boundary condition)",
line: func() Line {
l := NewLine()
l.SetFunctionIndex(2)
return l
}(),

src: func() ProfilesDictionary {
d := NewProfilesDictionary()
d.FunctionTable().AppendEmpty()
d.FunctionTable().AppendEmpty() // Length 2: indices 0,1 valid
return d
}(),
dst: NewProfilesDictionary(),

wantLine: func() Line {
l := NewLine()
l.SetFunctionIndex(2)
return l
}(),
wantDictionary: NewProfilesDictionary(),
wantErr: errors.New("invalid function index 2"),
},
} {
t.Run(tt.name, func(t *testing.T) {
line := tt.line
Expand Down
4 changes: 2 additions & 2 deletions pdata/pprofile/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (ms Location) Equal(val Location) bool {
// dictionary to another.
func (ms Location) switchDictionary(src, dst ProfilesDictionary) error {
if ms.MappingIndex() > 0 {
if src.MappingTable().Len() < int(ms.MappingIndex()) {
if src.MappingTable().Len() <= int(ms.MappingIndex()) {
return fmt.Errorf("invalid mapping index %d", ms.MappingIndex())
}

Expand All @@ -34,7 +34,7 @@ func (ms Location) switchDictionary(src, dst ProfilesDictionary) error {
}

for i, v := range ms.AttributeIndices().All() {
if src.AttributeTable().Len() < int(v) {
if src.AttributeTable().Len() <= int(v) {
return fmt.Errorf("invalid attribute index %d", v)
}

Expand Down
48 changes: 48 additions & 0 deletions pdata/pprofile/location_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,30 @@ func TestLocationSwitchDictionary(t *testing.T) {
wantDictionary: NewProfilesDictionary(),
wantErr: errors.New("invalid mapping index 1"),
},
{
name: "with a mapping index equal to the source table length (boundary condition)",
location: func() Location {
l := NewLocation()
l.SetMappingIndex(2)
return l
}(),

src: func() ProfilesDictionary {
d := NewProfilesDictionary()
d.MappingTable().AppendEmpty()
d.MappingTable().AppendEmpty()
return d
}(),
dst: NewProfilesDictionary(),

wantLocation: func() Location {
l := NewLocation()
l.SetMappingIndex(2)
return l
}(),
wantDictionary: NewProfilesDictionary(),
wantErr: errors.New("invalid mapping index 2"),
},
{
name: "with an existing attribute",
location: func() Location {
Expand Down Expand Up @@ -212,6 +236,30 @@ func TestLocationSwitchDictionary(t *testing.T) {
wantDictionary: NewProfilesDictionary(),
wantErr: errors.New("invalid attribute index 1"),
},
{
name: "with an attribute index equal to the source table length (boundary condition)",
location: func() Location {
l := NewLocation()
l.AttributeIndices().Append(2)
return l
}(),

src: func() ProfilesDictionary {
d := NewProfilesDictionary()
d.AttributeTable().AppendEmpty()
d.AttributeTable().AppendEmpty()
return d
}(),
dst: NewProfilesDictionary(),

wantLocation: func() Location {
l := NewLocation()
l.AttributeIndices().Append(2)
return l
}(),
wantDictionary: NewProfilesDictionary(),
wantErr: errors.New("invalid attribute index 2"),
},
{
name: "with an existing line",
location: func() Location {
Expand Down
4 changes: 2 additions & 2 deletions pdata/pprofile/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (ms Mapping) Equal(val Mapping) bool {
// dictionary to another.
func (ms Mapping) switchDictionary(src, dst ProfilesDictionary) error {
if ms.FilenameStrindex() > 0 {
if src.StringTable().Len() < int(ms.FilenameStrindex()) {
if src.StringTable().Len() <= int(ms.FilenameStrindex()) {
return fmt.Errorf("invalid filename index %d", ms.FilenameStrindex())
}

Expand All @@ -30,7 +30,7 @@ func (ms Mapping) switchDictionary(src, dst ProfilesDictionary) error {
}

for i, v := range ms.AttributeIndices().All() {
if src.AttributeTable().Len() < int(v) {
if src.AttributeTable().Len() <= int(v) {
return fmt.Errorf("invalid attribute index %d", v)
}

Expand Down
Loading
Loading