|
| 1 | +From 742921c9ba2854d27baa64272487fc5075d2c39c Mon Sep 17 00:00:00 2001 |
| 2 | +From: Mark Sagi-Kazar <mark.sagikazar@gmail.com> |
| 3 | +Date: Sat, 12 Jul 2025 07:25:50 +0200 |
| 4 | +Subject: [PATCH] fix: error message leaks |
| 5 | + |
| 6 | +Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com> |
| 7 | + |
| 8 | +Upstream Patch reference: https://github.com/go-viper/mapstructure/commit/742921c9ba2854d27baa64272487fc5075d2c39c.patch |
| 9 | +--- |
| 10 | + .../mitchellh/mapstructure/decode_hooks.go | 12 ++- |
| 11 | + .../mitchellh/mapstructure/error.go | 90 +++++++++++++++++++ |
| 12 | + .../mitchellh/mapstructure/mapstructure.go | 10 +-- |
| 13 | + 3 files changed, 103 insertions(+), 9 deletions(-) |
| 14 | + |
| 15 | +diff --git a/vendor/github.com/mitchellh/mapstructure/decode_hooks.go b/vendor/github.com/mitchellh/mapstructure/decode_hooks.go |
| 16 | +index 1f0abc6..4f70b03 100644 |
| 17 | +--- a/vendor/github.com/mitchellh/mapstructure/decode_hooks.go |
| 18 | ++++ b/vendor/github.com/mitchellh/mapstructure/decode_hooks.go |
| 19 | +@@ -113,7 +113,9 @@ func StringToTimeDurationHookFunc() DecodeHookFunc { |
| 20 | + } |
| 21 | + |
| 22 | + // Convert it by parsing |
| 23 | +- return time.ParseDuration(data.(string)) |
| 24 | ++ d, err := time.ParseDuration(data.(string)) |
| 25 | ++ |
| 26 | ++ return d, wrapTimeParseDurationError(err) |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | +@@ -134,7 +136,7 @@ func StringToIPHookFunc() DecodeHookFunc { |
| 31 | + // Convert it by parsing |
| 32 | + ip := net.ParseIP(data.(string)) |
| 33 | + if ip == nil { |
| 34 | +- return net.IP{}, fmt.Errorf("failed parsing ip %v", data) |
| 35 | ++ return net.IP{}, fmt.Errorf("failed parsing ip") |
| 36 | + } |
| 37 | + |
| 38 | + return ip, nil |
| 39 | +@@ -157,7 +159,7 @@ func StringToIPNetHookFunc() DecodeHookFunc { |
| 40 | + |
| 41 | + // Convert it by parsing |
| 42 | + _, net, err := net.ParseCIDR(data.(string)) |
| 43 | +- return net, err |
| 44 | ++ return net, wrapNetParseError(err) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | +@@ -176,7 +178,9 @@ func StringToTimeHookFunc(layout string) DecodeHookFunc { |
| 49 | + } |
| 50 | + |
| 51 | + // Convert it by parsing |
| 52 | +- return time.Parse(layout, data.(string)) |
| 53 | ++ ti, err := time.Parse(layout, data.(string)) |
| 54 | ++ |
| 55 | ++ return ti, wrapTimeParseError(err) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | +diff --git a/vendor/github.com/mitchellh/mapstructure/error.go b/vendor/github.com/mitchellh/mapstructure/error.go |
| 60 | +index 47a99e5..c5ac764 100644 |
| 61 | +--- a/vendor/github.com/mitchellh/mapstructure/error.go |
| 62 | ++++ b/vendor/github.com/mitchellh/mapstructure/error.go |
| 63 | +@@ -3,8 +3,11 @@ package mapstructure |
| 64 | + import ( |
| 65 | + "errors" |
| 66 | + "fmt" |
| 67 | ++ "net" |
| 68 | + "sort" |
| 69 | ++ "strconv" |
| 70 | + "strings" |
| 71 | ++ "time" |
| 72 | + ) |
| 73 | + |
| 74 | + // Error implements the error interface and can represents multiple |
| 75 | +@@ -48,3 +51,90 @@ func appendErrors(errors []string, err error) []string { |
| 76 | + return append(errors, e.Error()) |
| 77 | + } |
| 78 | + } |
| 79 | ++ |
| 80 | ++func wrapStrconvNumError(err error) error { |
| 81 | ++ if err == nil { |
| 82 | ++ return nil |
| 83 | ++ } |
| 84 | ++ |
| 85 | ++ if err, ok := err.(*strconv.NumError); ok { |
| 86 | ++ return &strconvNumError{Err: err} |
| 87 | ++ } |
| 88 | ++ |
| 89 | ++ return err |
| 90 | ++} |
| 91 | ++ |
| 92 | ++type strconvNumError struct { |
| 93 | ++ Err *strconv.NumError |
| 94 | ++} |
| 95 | ++ |
| 96 | ++func (e *strconvNumError) Error() string { |
| 97 | ++ return "strconv." + e.Err.Func + ": " + e.Err.Err.Error() |
| 98 | ++} |
| 99 | ++ |
| 100 | ++func (e *strconvNumError) Unwrap() error { return e.Err } |
| 101 | ++ |
| 102 | ++func wrapNetParseError(err error) error { |
| 103 | ++ if err == nil { |
| 104 | ++ return nil |
| 105 | ++ } |
| 106 | ++ |
| 107 | ++ if err, ok := err.(*net.ParseError); ok { |
| 108 | ++ return &netParseError{Err: err} |
| 109 | ++ } |
| 110 | ++ |
| 111 | ++ return err |
| 112 | ++} |
| 113 | ++ |
| 114 | ++type netParseError struct { |
| 115 | ++ Err *net.ParseError |
| 116 | ++} |
| 117 | ++ |
| 118 | ++func (e *netParseError) Error() string { |
| 119 | ++ return "invalid " + e.Err.Type |
| 120 | ++} |
| 121 | ++ |
| 122 | ++func (e *netParseError) Unwrap() error { return e.Err } |
| 123 | ++ |
| 124 | ++func wrapTimeParseError(err error) error { |
| 125 | ++ if err == nil { |
| 126 | ++ return nil |
| 127 | ++ } |
| 128 | ++ |
| 129 | ++ if err, ok := err.(*time.ParseError); ok { |
| 130 | ++ return &timeParseError{Err: err} |
| 131 | ++ } |
| 132 | ++ |
| 133 | ++ return err |
| 134 | ++} |
| 135 | ++ |
| 136 | ++type timeParseError struct { |
| 137 | ++ Err *time.ParseError |
| 138 | ++} |
| 139 | ++ |
| 140 | ++func (e *timeParseError) Error() string { |
| 141 | ++ if e.Err.Message == "" { |
| 142 | ++ return fmt.Sprintf("parsing time as %q: cannot parse as %q", e.Err.Layout, e.Err.LayoutElem) |
| 143 | ++ } |
| 144 | ++ |
| 145 | ++ return "parsing time " + e.Err.Message |
| 146 | ++} |
| 147 | ++ |
| 148 | ++func (e *timeParseError) Unwrap() error { return e.Err } |
| 149 | ++ |
| 150 | ++func wrapTimeParseDurationError(err error) error { |
| 151 | ++ if err == nil { |
| 152 | ++ return nil |
| 153 | ++ } |
| 154 | ++ |
| 155 | ++ errMsg := err.Error() |
| 156 | ++ if strings.HasPrefix(errMsg, "time: unknown unit ") { |
| 157 | ++ return errors.New("time: unknown unit") |
| 158 | ++ } else if strings.HasPrefix(errMsg, "time: ") { |
| 159 | ++ idx := strings.LastIndex(errMsg, " ") |
| 160 | ++ |
| 161 | ++ return errors.New(errMsg[:idx]) |
| 162 | ++ } |
| 163 | ++ |
| 164 | ++ return err |
| 165 | ++} |
| 166 | +diff --git a/vendor/github.com/mitchellh/mapstructure/mapstructure.go b/vendor/github.com/mitchellh/mapstructure/mapstructure.go |
| 167 | +index b384d9d..21c2264 100644 |
| 168 | +--- a/vendor/github.com/mitchellh/mapstructure/mapstructure.go |
| 169 | ++++ b/vendor/github.com/mitchellh/mapstructure/mapstructure.go |
| 170 | +@@ -592,7 +592,7 @@ func (d *Decoder) decodeInt(name string, data interface{}, val reflect.Value) er |
| 171 | + if err == nil { |
| 172 | + val.SetInt(i) |
| 173 | + } else { |
| 174 | +- return fmt.Errorf("cannot parse '%s' as int: %s", name, err) |
| 175 | ++ return fmt.Errorf("cannot parse '%s' as int: %s", name, wrapStrconvNumError(err)) |
| 176 | + } |
| 177 | + case dataType.PkgPath() == "encoding/json" && dataType.Name() == "Number": |
| 178 | + jn := data.(json.Number) |
| 179 | +@@ -644,14 +644,14 @@ func (d *Decoder) decodeUint(name string, data interface{}, val reflect.Value) e |
| 180 | + if err == nil { |
| 181 | + val.SetUint(i) |
| 182 | + } else { |
| 183 | +- return fmt.Errorf("cannot parse '%s' as uint: %s", name, err) |
| 184 | ++ return fmt.Errorf("cannot parse '%s' as uint: %s", name, wrapStrconvNumError(err)) |
| 185 | + } |
| 186 | + case dataType.PkgPath() == "encoding/json" && dataType.Name() == "Number": |
| 187 | + jn := data.(json.Number) |
| 188 | + i, err := jn.Int64() |
| 189 | + if err != nil { |
| 190 | + return fmt.Errorf( |
| 191 | +- "error decoding json.Number into %s: %s", name, err) |
| 192 | ++ "error decoding json.Number into %s: %s", name, wrapStrconvNumError(err)) |
| 193 | + } |
| 194 | + if i < 0 && !d.config.WeaklyTypedInput { |
| 195 | + return fmt.Errorf("cannot parse '%s', %d overflows uint", |
| 196 | +@@ -687,7 +687,7 @@ func (d *Decoder) decodeBool(name string, data interface{}, val reflect.Value) e |
| 197 | + } else if dataVal.String() == "" { |
| 198 | + val.SetBool(false) |
| 199 | + } else { |
| 200 | +- return fmt.Errorf("cannot parse '%s' as bool: %s", name, err) |
| 201 | ++ return fmt.Errorf("cannot parse '%s' as bool: %s", name, wrapStrconvNumError(err)) |
| 202 | + } |
| 203 | + default: |
| 204 | + return fmt.Errorf( |
| 205 | +@@ -721,7 +721,7 @@ func (d *Decoder) decodeFloat(name string, data interface{}, val reflect.Value) |
| 206 | + if err == nil { |
| 207 | + val.SetFloat(f) |
| 208 | + } else { |
| 209 | +- return fmt.Errorf("cannot parse '%s' as float: %s", name, err) |
| 210 | ++ return fmt.Errorf("cannot parse '%s' as float: %s", name, wrapStrconvNumError(err)) |
| 211 | + } |
| 212 | + case dataType.PkgPath() == "encoding/json" && dataType.Name() == "Number": |
| 213 | + jn := data.(json.Number) |
| 214 | +-- |
| 215 | +2.43.0 |
| 216 | + |
0 commit comments