From 9b7054fd0bc707f7f8e5c9ed9ada5a1664a19a34 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Fri, 31 Jan 2014 17:22:15 -0800 Subject: [PATCH] Do not generate all numberic truncated ids Fixes #3869 Docker-DCO-1.1-Signed-off-by: Michael Crosby (github: crosbymichael) --- image.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/image.go b/image.go index 7652824d49..b2d7f8eb49 100644 --- a/image.go +++ b/image.go @@ -203,12 +203,20 @@ func ValidateID(id string) error { } func GenerateID() string { - id := make([]byte, 32) - _, err := io.ReadFull(rand.Reader, id) - if err != nil { - panic(err) // This shouldn't happen + for { + id := make([]byte, 32) + if _, err := io.ReadFull(rand.Reader, id); err != nil { + panic(err) // This shouldn't happen + } + value := hex.EncodeToString(id) + // if we try to parse the truncated for as an int and we don't have + // an error then the value is all numberic and causes issues when + // used as a hostname. ref #3869 + if _, err := strconv.Atoi(utils.TruncateID(value)); err == nil { + continue + } + return value } - return hex.EncodeToString(id) } // Image includes convenience proxy functions to its graph