Skip to content

Commit 8ad0cf7

Browse files
committed
Adding error handling for the atomic types under overridable
Github: ZAP #1204
1 parent 0033a3c commit 8ad0cf7

File tree

2 files changed

+98
-40
lines changed

2 files changed

+98
-40
lines changed

src-electron/generator/overridable.js

Lines changed: 53 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -66,49 +66,62 @@ function nonAtomicType(arg = { name: 'unknown', isStruct: false }) {
6666
* @param {*} arg Object containing name and size
6767
*/
6868
function atomicType(arg = { name: 'unknown', size: 0, no_warning: 0 }) {
69-
let name = arg.name
70-
let size = arg.size
71-
let no_warning = arg.no_warning
72-
if (name.startsWith('int')) {
73-
let signed
74-
if (name.endsWith('s')) signed = true
75-
else signed = false
69+
try {
70+
let name = arg.name
71+
let size = arg.size
72+
let no_warning = arg.no_warning
73+
if (!name || typeof name !== 'string') {
74+
throw new Error(`Invalid or empty type name ${name}.`)
75+
}
76+
// Regex to catch invalid Zigbee type names like uint16, int16, etc.
77+
if (/^(u?int\d+)$/i.test(name)) {
78+
throw new Error(
79+
`Invalid Zigbee type name ${name}. Did you mean "int16u", "int16s", "int32u", etc.?`
80+
)
81+
}
82+
if (name.startsWith('int')) {
83+
let signed
84+
if (name.endsWith('s')) signed = true
85+
else signed = false
7686

77-
let ret = `${signed ? '' : 'u'}int${size * 8}_t`
87+
let ret = `${signed ? '' : 'u'}int${size * 8}_t`
7888

79-
// few exceptions
80-
ret = cleanseUints(ret, size * 8, signed)
81-
return ret
82-
} else if (name.startsWith('enum') || name.startsWith('data')) {
83-
return cleanseUints(`uint${name.slice(4)}_t`, name.slice(4), false)
84-
} else if (name.startsWith('bitmap')) {
85-
return cleanseUints(`uint${name.slice(6)}_t`, name.slice(6), false)
86-
} else {
87-
switch (name) {
88-
case 'utc_time':
89-
case 'date':
90-
case 'time_of_day':
91-
case 'bacnet_oid':
92-
return 'uint32_t'
93-
case 'attribute_id':
94-
case 'cluster_id':
95-
return 'uint16_t'
96-
case 'no_data':
97-
case 'octet_string':
98-
case 'char_string':
99-
case 'ieee_address':
100-
return 'uint8_t *'
101-
case 'boolean':
102-
return 'uint8_t'
103-
case 'array':
104-
return no_warning
105-
? `uint8_t *`
106-
: `/* TYPE WARNING: ${name} array defaults to */ uint8_t * `
107-
default:
108-
return no_warning
109-
? `uint8_t *`
110-
: `/* TYPE WARNING: ${name} defaults to */ uint8_t * `
89+
// few exceptions
90+
ret = cleanseUints(ret, size * 8, signed)
91+
return ret
92+
} else if (name.startsWith('enum') || name.startsWith('data')) {
93+
return cleanseUints(`uint${name.slice(4)}_t`, name.slice(4), false)
94+
} else if (name.startsWith('bitmap')) {
95+
return cleanseUints(`uint${name.slice(6)}_t`, name.slice(6), false)
96+
} else {
97+
switch (name) {
98+
case 'utc_time':
99+
case 'date':
100+
case 'time_of_day':
101+
case 'bacnet_oid':
102+
return 'uint32_t'
103+
case 'attribute_id':
104+
case 'cluster_id':
105+
return 'uint16_t'
106+
case 'no_data':
107+
case 'octet_string':
108+
case 'char_string':
109+
case 'ieee_address':
110+
return 'uint8_t *'
111+
case 'boolean':
112+
return 'uint8_t'
113+
case 'array':
114+
return no_warning
115+
? `uint8_t *`
116+
: `/* TYPE WARNING: ${name} array defaults to */ uint8_t * `
117+
default:
118+
return no_warning
119+
? `uint8_t *`
120+
: `/* TYPE WARNING: ${name} defaults to */ uint8_t * `
121+
}
111122
}
123+
} catch (err) {
124+
throw new Error('atomicType failed for type ' + ': ' + err.message)
112125
}
113126
}
114127

test/helpers.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const zclLoader = require('../src-electron/zcl/zcl-loader')
2727
const zclHelper = require('../src-electron/generator/helper-zcl')
2828
const dbEnum = require('../src-shared/db-enum')
2929
const zapHelper = require('../src-electron/generator/helper-zap')
30+
const { atomicType } = require('../src-electron/generator/overridable')
3031

3132
let db
3233
let zclContext
@@ -634,3 +635,47 @@ test(
634635
},
635636
testUtil.timeout.short()
636637
)
638+
639+
test(
640+
'Atomic Type test 1',
641+
() => {
642+
const attribute = {
643+
name: '', // This must be empty to trigger the error
644+
code: 0x4011,
645+
define: 'COLOR_CONTROL_ATTRIBUTE_TEST',
646+
type: '',
647+
side: 'server',
648+
min: '0x0000',
649+
max: '0xFFFF',
650+
writable: false,
651+
optional: true,
652+
xmlFile: 'zcl-builtin/matter/data-model/chip/color-control-cluster.xml'
653+
}
654+
expect(() => atomicType(attribute)).toThrow(
655+
/atomicType failed for type : Invalid or empty type name ./
656+
)
657+
},
658+
testUtil.timeout.short()
659+
)
660+
661+
test(
662+
'Atomic Type test 2',
663+
() => {
664+
const attribute = {
665+
name: 'uint16', // This must start with uint to trigger the error
666+
code: 0x4011,
667+
define: 'COLOR_CONTROL_ATTRIBUTE_TEST',
668+
type: '',
669+
side: 'server',
670+
min: '0x0000',
671+
max: '0xFFFF',
672+
writable: false,
673+
optional: true,
674+
xmlFile: 'zcl-builtin/matter/data-model/chip/color-control-cluster.xml'
675+
}
676+
expect(() => atomicType(attribute)).toThrow(
677+
/atomicType failed for type : Invalid Zigbee type name uint16. Did you mean "int16u", "int16s", "int32u", etc.?/
678+
)
679+
},
680+
testUtil.timeout.short()
681+
)

0 commit comments

Comments
 (0)