-int bio_read(opj_bio_t *bio, int n) {
- int i, v;
- v = 0;
- for (i = n - 1; i >= 0; i--) {
- v += opj_bio_getbit(bio) << i;
- }
- return v;
+OPJ_UINT32 opj_bio_read(opj_bio_t *bio, OPJ_UINT32 n)
+{
+ OPJ_INT32 i;
+ OPJ_UINT32 v;
+
+ assert((n > 0U) /* && (n <= 32U)*/);
+#ifdef OPJ_UBSAN_BUILD
+ /* This assert fails for some corrupted images which are gracefully rejected */
+ /* Add this assert only for ubsan build. */
+ /* This is the condition for overflow not to occur below which is needed because of OPJ_NOSANITIZE */
+ assert(n <= 32U);
+#endif
+ v = 0U;
+ for (i = (OPJ_INT32)n - 1; i >= 0; i--) {
+ v |= opj_bio_getbit(bio) <<
+ i; /* can't overflow, opj_bio_getbit returns 0 or 1 */
+ }
+ return v;