Skip to content

Commit

Permalink
fix: add check to avoid illegal index make out of bounds
Browse files Browse the repository at this point in the history
Signed-off-by: PennyYoon <525296438@qq.com>
  • Loading branch information
Chenxulin97 committed Nov 22, 2023
1 parent b89fdc0 commit 1266ebb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 4 additions & 0 deletions opengemini/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package opengemini

import (
"encoding/base64"
"errors"
"io"
"net/http"
"net/url"
Expand Down Expand Up @@ -42,6 +43,9 @@ func (c *client) executeHttpGetByIdx(idx int, urlPath string, details requestDet
}

func (c *client) executeHttpRequestByIdx(idx int, method, urlPath string, details requestDetails) (*http.Response, error) {
if idx >= len(c.serverUrls) || idx < 0 {
return nil, errors.New("index out of range")
}
serverUrl := c.serverUrls[idx]
return c.executeHttpRequest(method, serverUrl, urlPath, details)
}
Expand Down
16 changes: 15 additions & 1 deletion opengemini/ping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestPingSuccess(t *testing.T) {
require.Nil(t, err)
}

func TestPingFail(t *testing.T) {
func TestPingFailForInaccessibleAddress(t *testing.T) {
c := testNewClient(t, &Config{
Addresses: []*Address{{
Host: "localhost",
Expand All @@ -31,3 +31,17 @@ func TestPingFail(t *testing.T) {
err := c.Ping(1)
require.NotNil(t, err)
}

func TestPingFailForOutOfBounds(t *testing.T) {
c := testNewClient(t, &Config{
Addresses: []*Address{{
Host: "localhost",
Port: 8086,
}},
})

err := c.Ping(1)
require.NotNil(t, err)
err = c.Ping(-1)
require.NotNil(t, err)
}

0 comments on commit 1266ebb

Please sign in to comment.