Skip to content

Commit f2acd76

Browse files
bellardsaghul
authored andcommitted
Fix UB in bf_set_ui()
Ref: bellard/quickjs#131
1 parent 6b3bed1 commit f2acd76

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

libbf.c

+16-16
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,21 @@ static inline slimb_t sat_add(slimb_t a, slimb_t b)
164164
return r;
165165
}
166166

167+
static inline __maybe_unused limb_t shrd(limb_t low, limb_t high, long shift)
168+
{
169+
if (shift != 0)
170+
low = (low >> shift) | (high << (LIMB_BITS - shift));
171+
return low;
172+
}
173+
174+
static inline __maybe_unused limb_t shld(limb_t a1, limb_t a0, long shift)
175+
{
176+
if (shift != 0)
177+
return (a1 << shift) | (a0 >> (LIMB_BITS - shift));
178+
else
179+
return a1;
180+
}
181+
167182
#define malloc(s) malloc_is_forbidden(s)
168183
#define free(p) free_is_forbidden(p)
169184
#define realloc(p, s) realloc_is_forbidden(p, s)
@@ -236,7 +251,7 @@ int bf_set_ui(bf_t *r, uint64_t a)
236251
a1 = a >> 32;
237252
shift = clz(a1);
238253
r->tab[0] = a0 << shift;
239-
r->tab[1] = (a1 << shift) | (a0 >> (LIMB_BITS - shift));
254+
r->tab[1] = shld(a1, a0, shift);
240255
r->expn = 2 * LIMB_BITS - shift;
241256
}
242257
#endif
@@ -5388,21 +5403,6 @@ int bf_acos(bf_t *r, const bf_t *a, limb_t prec, bf_flags_t flags)
53885403

53895404
#endif /* LIMB_BITS != 64 */
53905405

5391-
static inline __maybe_unused limb_t shrd(limb_t low, limb_t high, long shift)
5392-
{
5393-
if (shift != 0)
5394-
low = (low >> shift) | (high << (LIMB_BITS - shift));
5395-
return low;
5396-
}
5397-
5398-
static inline __maybe_unused limb_t shld(limb_t a1, limb_t a0, long shift)
5399-
{
5400-
if (shift != 0)
5401-
return (a1 << shift) | (a0 >> (LIMB_BITS - shift));
5402-
else
5403-
return a1;
5404-
}
5405-
54065406
#if LIMB_DIGITS == 19
54075407

54085408
/* WARNING: hardcoded for b = 1e19. It is assumed that:

0 commit comments

Comments
 (0)