From 8f50d2dd9a5b943881f0827fc07fbfb389e1407c Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 4 Nov 2013 14:20:26 -0800 Subject: [PATCH] builder/virtualbox: no panic if ssh host port min/max is same [GH-594] --- CHANGELOG.md | 1 + builder/virtualbox/step_forward_ssh.go | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d9bf33e84..7e0a6f771 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ BUG FIXES: * builder/amazon/all: Properly scrub access key and secret key from logs. [GH-554] * builder/openstack: Properly scrub password from logs [GH-554] +* builder/virtualbox: No panic if SSH host port min/max is the same. [GH-594] * common/uuid: Use cryptographically secure PRNG when generating UUIDs. [GH-552] * communicator/ssh: File uploads that exceed the size of memory no longer diff --git a/builder/virtualbox/step_forward_ssh.go b/builder/virtualbox/step_forward_ssh.go index 5ea4ed3d5..55a46611e 100644 --- a/builder/virtualbox/step_forward_ssh.go +++ b/builder/virtualbox/step_forward_ssh.go @@ -25,9 +25,16 @@ func (s *stepForwardSSH) Run(state multistep.StateBag) multistep.StepAction { log.Printf("Looking for available SSH port between %d and %d", config.SSHHostPortMin, config.SSHHostPortMax) var sshHostPort uint + var offset uint = 0 + portRange := int(config.SSHHostPortMax - config.SSHHostPortMin) + if portRange > 0 { + // Have to check if > 0 to avoid a panic + offset = uint(rand.Intn(portRange)) + } + for { - sshHostPort = uint(rand.Intn(portRange)) + config.SSHHostPortMin + sshHostPort = offset + config.SSHHostPortMin log.Printf("Trying port: %d", sshHostPort) l, err := net.Listen("tcp", fmt.Sprintf(":%d", sshHostPort)) if err == nil {