aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRose Hogenson <rosehogenson@posteo.net>2025-11-24 16:06:33 -0800
committerRose Hogenson <rosehogenson@posteo.net>2025-11-24 16:16:33 -0800
commitc61422f65bf1859da9336d764bb328b1882ec287 (patch)
treed8f1b320e000baa36d01b9e531b54f43c1a98ca7
parentda798b651e8efc766cf66a39ccef906681f0b17a (diff)
downloadccl-c61422f65bf1859da9336d764bb328b1882ec287.tar.zst
Don't allow decoding bool as int
-rw-r--r--ccl.go18
-rw-r--r--ccl_test.go40
2 files changed, 26 insertions, 32 deletions
diff --git a/ccl.go b/ccl.go
index 6753dbc..eea772c 100644
--- a/ccl.go
+++ b/ccl.go
@@ -687,24 +687,10 @@ func intLimits(kind reflect.Kind) (min, max uint64, ok bool) {
func (p *parser) unpackBool(fieldVal reflect.Value, b bool, field []byte) error {
fieldVal = setPtr(fieldVal)
- switch fieldVal.Kind() {
- case reflect.Bool:
- fieldVal.SetBool(b)
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- if b {
- fieldVal.SetInt(1)
- } else {
- fieldVal.SetInt(0)
- }
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
- if b {
- fieldVal.SetUint(1)
- } else {
- fieldVal.SetUint(0)
- }
- default:
+ if fieldVal.Kind() != reflect.Bool {
return p.error("field %q should have type bool", field)
}
+ fieldVal.SetBool(b)
return nil
}
diff --git a/ccl_test.go b/ccl_test.go
index 6e3ef3f..15d75af 100644
--- a/ccl_test.go
+++ b/ccl_test.go
@@ -172,22 +172,6 @@ can just span multiple lines"`,
msg: `uint64:1`,
want: message{Uint64: 1},
}, {
- desc: "IntTrue",
- msg: `int:on`,
- want: message{Int: 1},
- }, {
- desc: "IntFalse",
- msg: `int:no`,
- want: message{Int: 0},
- }, {
- desc: "UintTrue",
- msg: `uint:on`,
- want: message{Uint: 1},
- }, {
- desc: "UintFalse",
- msg: `uint:no`,
- want: message{Uint: 0},
- }, {
desc: "IntFloat",
msg: `float:-1`,
want: message{Float: -1},
@@ -605,6 +589,30 @@ func TestUnmarshal_InvalidType(t *testing.T) {
desc: "RepeatedSingular",
msg: `F:[1]`,
out: new(struct{ F int }),
+ }, {
+ desc: "IntTrue",
+ msg: `int:on`,
+ out: new(struct {
+ F int `ccl:"int"`
+ }),
+ }, {
+ desc: "IntFalse",
+ msg: `int:no`,
+ out: new(struct {
+ F int `ccl:"int"`
+ }),
+ }, {
+ desc: "UintTrue",
+ msg: `uint:on`,
+ out: new(struct {
+ F uint `ccl:"uint"`
+ }),
+ }, {
+ desc: "UintFalse",
+ msg: `uint:no`,
+ out: new(struct {
+ F uint `ccl:"uint"`
+ }),
}} {
t.Run(tc.desc, func(t *testing.T) {
t.Parallel()