simpletlv: Avoid writing before all sanity checks in sc_simpletlv_put_tag()

This commit is contained in:
Jakub Jelen 2019-11-15 18:35:41 +01:00
parent b0d3a70b91
commit 8fd5ffd54e
1 changed files with 5 additions and 4 deletions

View File

@ -49,19 +49,20 @@ sc_simpletlv_put_tag(u8 tag, size_t datalen, u8 *out, size_t outlen, u8 **ptr)
/* tag is just number between 0x01 and 0xFE */
if (tag == 0x00 || tag == 0xff)
return SC_ERROR_INVALID_ARGUMENTS;
if (datalen > 0xffff) {
/* we can't store more than two bytes in Simple TLV */
return SC_ERROR_WRONG_LENGTH;
}
*p++ = tag; /* tag is single byte */
if (datalen < 0xff) {
/* short value up to 255 */
*p++ = (u8)datalen; /* is in the second byte */
} else if (datalen < 0xffff) {
} else {
/* longer values up to 65535 */
*p++ = (u8)0xff; /* first byte is 0xff */
*p++ = (u8)datalen & 0xff;
*p++ = (u8)(datalen >> 8) & 0xff; /* LE */
} else {
/* we can't store more than two bytes in Simple TLV */
return SC_ERROR_WRONG_LENGTH;
}
if (ptr != NULL)
*ptr = p;