@cryptotaxi247 / kubo / commits / 44e005e57

go-ipfs-config: fix: panic on invalid priority/flag values

Steven Allen committed Jun 16, 2020 at 10:17 UTC 44e005e57709ee7e1b95270476d8a32aa0d23076
1 file changed +18 -3
config/types.go
+18 -3
@@ -56,6 +56,8 @@ const (
56 )
57
58 // WithDefault resolves the value of the flag given the provided default value.
59 +//
60 +// Panics if Flag is an invalid value.
61 func (f Flag) WithDefault(defaultValue bool) bool {
62 switch f {
63 case False:
@@ -126,17 +128,30 @@ const (
128
129 // WithDefault resolves the priority with the given default.
130 //
129 -// If `defaultPriority` is Default/0, this function will return 0.
131 +// If defaultPriority is Default/0, this function will return 0.
132 +//
133 +// Panics if the priority has an invalid value (e.g., not DefaultPriority,
134 +// Disabled, or > 0).
135 func (p Priority) WithDefault(defaultPriority Priority) (priority int64, enabled bool) {
136 switch p {
137 case Disabled:
138 return 0, false
139 case DefaultPriority:
135 - if defaultPriority < 0 {
140 + switch defaultPriority {
141 + case Disabled:
142 return 0, false
143 + case DefaultPriority:
144 + return 0, true
145 + default:
146 + if defaultPriority <= 0 {
147 + panic(fmt.Sprintf("invalid priority %d < 0", int64(defaultPriority)))
148 + }
149 + return int64(defaultPriority), true
150 }
138 - return int64(defaultPriority), true
151 default:
152 + if p <= 0 {
153 + panic(fmt.Sprintf("invalid priority %d < 0", int64(p)))
154 + }
155 return int64(p), true
156 }
157 }