Skip to content

Commit

Permalink
be more liberal with [...] in attr definitions
Browse files Browse the repository at this point in the history
and add tests for struct
  • Loading branch information
alandekok committed May 18, 2016
1 parent 7cdcaa1 commit 5c11463
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
41 changes: 23 additions & 18 deletions src/lib/dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -1490,34 +1490,39 @@ static int dict_read_process_attribute(fr_dict_t *dict, fr_dict_attr_t const *pa
block_vendor = vendor; /* Weird case where we're processing 26.<vid>.<tlv> */
}

if (strncmp(argv[2], "octets[", 7) != 0) {
/*
* find the type of the attribute.
*/
type = fr_str2int(dict_attr_types, argv[2], -1);
if (type < 0) {
fr_strerror_printf("Unknown data type '%s'", argv[2]);
return -1;
}
/*
* Some types can have fixed length
*/
p = strchr(argv[2], '[');
if (p) *p = '\0';

} else {
type = PW_TYPE_OCTETS;
/*
* find the type of the attribute.
*/
type = fr_str2int(dict_attr_types, argv[2], -1);
if (type < 0) {
fr_strerror_printf("Unknown data type '%s'", argv[2]);
return -1;
}

if (p) {
char *q;

p = strchr(argv[2] + 7, ']');
if (!p) {
fr_strerror_printf("Invalid format for 'octets'");
q = strchr(p + 1, ']');
if (!q) {
fr_strerror_printf("Invalid format for '%s[...]'", argv[2]);
return -1;
}

*p = 0;
*q = 0;

if (!dict_read_sscanf_i(argv[2] + 7, &length)) {
fr_strerror_printf("Invalid length for 'octets'");
if (!dict_read_sscanf_i(p + 1, &length)) {
fr_strerror_printf("Invalid length for '%s[...]'", argv[2]);
return -1;
}

if ((length == 0) || (length > 253)) {
fr_strerror_printf("Invalid length for 'octets'");
fr_strerror_printf("Invalid length for '%s[...]'", argv[2]);
return -1;
}

Expand Down
19 changes: 17 additions & 2 deletions src/tests/unit/dict.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ dictionary ATTRIBUTE Unit-Fail 241.243.255.1 integer
data Unknown child attribute starting at "255.1"

dictionary ATTRIBUTE Unit-Array 255 octets[123458]
data Invalid length for 'octets'
data Invalid length for 'octets[...]'

dictionary ATTRIBUTE Unit-Integer 255 integer[1]
data fr_dict_attr_add: Failed adding 'Unit-Integer': The 'length' flag can only be set for attributes of type 'octets'

dictionary ATTRIBUTE Unit-$bad$stuff 255 integer
data Invalid character '$' in attribute name
Expand All @@ -60,7 +63,7 @@ data Invalid character '$' in attribute name
# flags
#
dictionary ATTRIBUTE Unit-Array 241.254 octets[1234]
data Invalid length for 'octets'
data Invalid length for 'octets[...]'

dictionary ATTRIBUTE Unit-Tag 241.255 integer has_tag
data fr_dict_attr_add: Failed adding 'Unit-Tag': The 'has_tag' flag can only be used with RFC and VSA attributes
Expand All @@ -70,3 +73,15 @@ data fr_dict_attr_add: Failed adding 'Unit-Tag': The 'has_tag' flag can only be

dictionary ATTRIBUTE Unit-Tag 255 tlv encrypt=1
data fr_dict_attr_add: Failed adding 'Unit-Tag': The 'encrypt=1' flag can only be used with attributes of type 'string'

#
# structs
#
dictionary ATTRIBUTE Unit-Struct 241.254 struct
data ok

dictionary ATTRIBUTE Unit-Struct-Byte 241.254.1 byte
data ok

dictionary ATTRIBUTE Unit-Struct-Octets 241.254.2 octets[2]
data ok

0 comments on commit 5c11463

Please sign in to comment.