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

Fix "slice bounds out of range" error in memoryMetric.selectPoints() #30

Merged
merged 1 commit into from
Sep 6, 2021

Conversation

fedorlitau
Copy link
Contributor

This will fix a bug which I have encountered while using nakabonne/ali (awesome! btw):

panic: runtime error: slice bounds out of range [6:1]

goroutine 53 [running]:in:

github.com/nakabonne/tstorage.(*memoryMetric).selectPoints(0xc0001ba7e0, 0x16a06b537e57044c, 0x16a06b5a7a7ab04c, 0x0, 0x0, 0x0)
        /root/go/pkg/mod/github.com/nakabonne/tstorage@v0.2.0/memory_partition.go:232 +0x27b
github.com/nakabonne/tstorage.(*memoryPartition).selectDataPoints(0xc000132200, 0x82dbf7, 0x7, 0x0, 0x0, 0x0, 0x16a06b537e57044c, 0x16a06b5a7a7ab04c, 0x449a4c, 0xc000092238, ...)
        /root/go/pkg/mod/github.com/nakabonne/tstorage@v0.2.0/memory_partition.go:124 +0x9e
github.com/nakabonne/tstorage.(*storage).Select(0xc000132180, 0x82dbf7, 0x7, 0x0, 0x0, 0x0, 0x16a06b537e57044c, 0x16a06b5a7a7ab04c, 0x46a77b, 0x77b3455e6fa8, ...)
        /root/go/pkg/mod/github.com/nakabonne/tstorage@v0.2.0/storage.go:324 +0x262
github.com/nakabonne/ali/storage.(*storage).Select(0xc00012e260, 0x82dbf7, 0x7, 0xc043adf3db12ae4c, 0x323d8539, 0xb456e0, 0xc043adfb5b12ae4c, 0x72e613139, 0xb456e0, 0x0, ...)
        /opt/ali/storage/storage.go:116 +0x105
github.com/nakabonne/ali/gui.(*drawer).redrawCharts(0xc0001c6480, 0x8a5ab8, 0xc00007e000)
        /opt/ali/gui/drawer.go:53 +0x262
created by github.com/nakabonne/ali/gui.attack
        /opt/ali/gui/keybinds.go:55 +0x9d

In the original code almost always the first case (green) is entered - the bug occurs when the second (red) case is executed:

image

@nakabonne
Copy link
Owner

@fedorlitau Thank you for your contribution! Sorry I got late. I'm in the middle of traveling so let me take a look later.

Btw, seems like your git commits aren't linked to your Github account (Ali's PR either). Because I want to accept you as a contributor, could you configure that setting if possible?
image

@fedorlitau
Copy link
Contributor Author

@nakabonne Good point - I've corrected the commits on both PR's. Enjoy the traveling! ;-)

Copy link
Owner

@nakabonne nakabonne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well noticed! Thank you for working on this problem ;)

memory_partition.go Outdated Show resolved Hide resolved
@nakabonne
Copy link
Owner

@fedorlitau In addition to that, could you add this test case to memory_partition_test.go?

diff --git a/memory_partition_test.go b/memory_partition_test.go
index 625d0bc..0e0701b 100644
--- a/memory_partition_test.go
+++ b/memory_partition_test.go
@@ -84,7 +84,43 @@ func Test_memoryPartition_SelectDataPoints(t *testing.T) {
                        want:            []*DataPoint{},
                },
                {
-                       name:   "select multiple points",
+                       name:   "select some points",
+                       metric: "metric1",
+                       start:  2,
+                       end:    4,
+                       memoryPartition: func() *memoryPartition {
+                               m := newMemoryPartition(nil, 0, "").(*memoryPartition)
+                               m.insertRows([]Row{
+                                       {
+                                               Metric:    "metric1",
+                                               DataPoint: DataPoint{Timestamp: 1, Value: 0.1},
+                                       },
+                                       {
+                                               Metric:    "metric1",
+                                               DataPoint: DataPoint{Timestamp: 2, Value: 0.1},
+                                       },
+                                       {
+                                               Metric:    "metric1",
+                                               DataPoint: DataPoint{Timestamp: 3, Value: 0.1},
+                                       },
+                                       {
+                                               Metric:    "metric1",
+                                               DataPoint: DataPoint{Timestamp: 4, Value: 0.1},
+                                       },
+                                       {
+                                               Metric:    "metric1",
+                                               DataPoint: DataPoint{Timestamp: 5, Value: 0.1},
+                                       },
+                               })
+                               return m
+                       }(),
+                       want: []*DataPoint{
+                               {Timestamp: 2, Value: 0.1},
+                               {Timestamp: 3, Value: 0.1},
+                       },
+               },
+               {
+                       name:   "select all points",
                        metric: "metric1",
                        start:  1,
                        end:    4,

@codecov-commenter
Copy link

codecov-commenter commented Sep 6, 2021

Codecov Report

Merging #30 (b92d1c9) into main (9034eef) will increase coverage by 0.74%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #30      +/-   ##
==========================================
+ Coverage   65.35%   66.08%   +0.74%     
==========================================
  Files          19       19              
  Lines         955      955              
==========================================
+ Hits          624      631       +7     
+ Misses        234      229       -5     
+ Partials       97       95       -2     
Impacted Files Coverage Δ
memory_partition.go 83.34% <100.00%> (+5.31%) ⬆️

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 9034eef...b92d1c9. Read the comment docs.

Copy link
Owner

@nakabonne nakabonne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

@nakabonne nakabonne merged commit 2ad4f71 into nakabonne:main Sep 6, 2021
@fedorlitau
Copy link
Contributor Author

Thank you! And thank you for incorporating the fixes quicky and for: ali 💪😀

@github-actions github-actions bot mentioned this pull request Sep 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants