Skip to content

Commit d1ca3e0

Browse files
committed
lyb BUGFIX make sure written hash is not 0
1 parent 7ba2069 commit d1ca3e0

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

src/lyb.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,26 @@ lyb_prepend_bits(void *buf, uint32_t count_bytes, uint8_t byte, uint8_t byte_bit
219219
/* prepend the bits from the byte */
220220
((uint8_t *)buf)[0] |= byte & lyb_right_bit_mask(byte_bits);
221221
}
222+
223+
uint32_t
224+
lyb_truncate_hash_nonzero(uint32_t hash, uint8_t hash_bits)
225+
{
226+
uint32_t orig_hash = hash;
227+
uint8_t offset;
228+
229+
assert(hash && !(hash_bits % 8));
230+
231+
offset = 0;
232+
do {
233+
hash = orig_hash;
234+
235+
/* truncate hash */
236+
hash <<= (32 - offset) - hash_bits;
237+
hash >>= 32 - hash_bits;
238+
239+
/* next offset */
240+
offset += 8;
241+
} while (!hash);
242+
243+
return hash;
244+
}

src/lyb.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,15 @@ void lyb_right_shift(void *buf, uint32_t count_bytes, uint8_t shift);
255255
*/
256256
void lyb_prepend_bits(void *buf, uint32_t count_bytes, uint8_t byte, uint8_t byte_bits);
257257

258+
/**
259+
* @brief Truncate a hash to rightmost bits and make sure it is non-zero.
260+
*
261+
* If these bits are all zeroes, next bits are used.
262+
*
263+
* @param[in] hash Hash to truncate.
264+
* @param[in] hash_bits Size of the truncate hash in bits.
265+
* @return Truncated non-zero hash.
266+
*/
267+
uint32_t lyb_truncate_hash_nonzero(uint32_t hash, uint8_t hash_bits);
268+
258269
#endif /* LY_LYB_H_ */

src/parser_lyb.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,9 +1542,7 @@ lyb_parse_header(struct lylyb_parse_ctx *lybctx)
15421542
lybctx->empty_hash = 1;
15431543
} else {
15441544
/* truncate context hash to the same bit size */
1545-
cur_hash = ly_ctx_get_modules_hash(lybctx->ctx);
1546-
cur_hash <<= (sizeof cur_hash * 8) - LYB_HEADER_CTX_HASH_BITS;
1547-
cur_hash >>= (sizeof cur_hash * 8) - LYB_HEADER_CTX_HASH_BITS;
1545+
cur_hash = lyb_truncate_hash_nonzero(ly_ctx_get_modules_hash(lybctx->ctx), LYB_HEADER_CTX_HASH_BITS);
15481546

15491547
if (data_hash != cur_hash) {
15501548
LOGERR(lybctx->ctx, LY_EINVAL, "Different current LYB context modules hash compared to the one stored in the "

src/printer_lyb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ lyb_print_header(struct lylyb_print_ctx *lybctx)
634634

635635
/* context hash (is truncated), if not printing empty data */
636636
if (lybctx->ctx) {
637-
hash = ly_ctx_get_modules_hash(lybctx->ctx);
637+
hash = lyb_truncate_hash_nonzero(ly_ctx_get_modules_hash(lybctx->ctx), LYB_HEADER_CTX_HASH_BITS);
638638
} else {
639639
hash = 0;
640640
}

0 commit comments

Comments
 (0)