Skip to content

Commit

Permalink
Address CodeQL warning
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebakken committed Mar 5, 2024
1 parent 7305875 commit d968382
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 17 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ jobs:
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -51,7 +51,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2
uses: github/codeql-action/autobuild@v3

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
Expand All @@ -65,4 +65,4 @@ jobs:
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
uses: github/codeql-action/analyze@v3
6 changes: 3 additions & 3 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ jobs:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
- uses: actions/setup-go@v5
with:
go-version: 'stable'
check-latest: true
- uses: actions/checkout@v3
- uses: golangci/golangci-lint-action@v3
- uses: actions/checkout@v4
- uses: golangci/golangci-lint-action@v4
with:
version: latest
only-new-issues: false
8 changes: 4 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ jobs:
matrix:
go-version: ['oldstable', 'stable']
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
check-latest: true
Expand All @@ -43,8 +43,8 @@ jobs:
ports:
- 5672:5672
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
check-latest: true
Expand Down
32 changes: 27 additions & 5 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const (
platform = "golang"
// Safer default that makes channel leaks a lot easier to spot
// before they create operational headaches. See https://github.com/rabbitmq/rabbitmq-server/issues/1593.
defaultChannelMax = (2 << 10) - 1
defaultChannelMax = uint16((2 << 10) - 1)
defaultLocale = "en_US"
)

Expand All @@ -49,7 +49,7 @@ type Config struct {
// bindings on the server. Dial sets this to the path parsed from the URL.
Vhost string

ChannelMax int // 0 max channels means 2^16 - 1
ChannelMax uint16 // 0 max channels means 2^16 - 1
FrameSize int // 0 max bytes means unlimited
Heartbeat time.Duration // less than 1s uses the server's interval

Expand Down Expand Up @@ -991,13 +991,13 @@ func (c *Connection) openTune(config Config, auth Authentication) error {

// When the server and client both use default 0, then the max channel is
// only limited by uint16.
c.Config.ChannelMax = pick(config.ChannelMax, int(tune.ChannelMax))
c.Config.ChannelMax = pickUInt16(config.ChannelMax, tune.ChannelMax)
if c.Config.ChannelMax == 0 {
c.Config.ChannelMax = defaultChannelMax
}
c.Config.ChannelMax = min(c.Config.ChannelMax, maxChannelMax)
c.Config.ChannelMax = minUInt16(c.Config.ChannelMax, maxChannelMax)

c.allocator = newAllocator(1, c.Config.ChannelMax)
c.allocator = newAllocator(1, int(c.Config.ChannelMax))

c.m.Unlock()

Expand Down Expand Up @@ -1104,13 +1104,35 @@ func max(a, b int) int {
return b
}

func maxUInt16(a, b uint16) uint16 {
if a > b {
return a
}
return b
}

func min(a, b int) int {
if a < b {
return a
}
return b
}

func minUInt16(a, b uint16) uint16 {
if a < b {
return a
}
return b
}

func pickUInt16(client, server uint16) uint16 {
if client == 0 || server == 0 {
return maxUInt16(client, server)
} else {
return minUInt16(client, server)
}
}

func pick(client, server int) int {
if client == 0 || server == 0 {
return max(client, server)
Expand Down
2 changes: 1 addition & 1 deletion integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ func TestIntegrationChannelIDsExhausted(t *testing.T) {
}
defer c.Close()

for i := 1; i <= c.Config.ChannelMax; i++ {
for i := uint16(1); i <= c.Config.ChannelMax; i++ {
if _, err := c.Channel(); err != nil {
t.Fatalf("expected allocating all channel ids to succed, failed on %d with %v", i, err)
}
Expand Down

0 comments on commit d968382

Please sign in to comment.