← Commit list

8bb03da90

Denys Vlasenko
Denys Vlasenko
2021-07-02
SUCCESS Duration: 153s

Subject

awk: rand() could return 1.0, fix this - should be in [0,1)

Binary

No binary (build failed)

Architecture & Compiler Flags

Architecture: x86_64-linux-gnu Compiler: gcc (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0

—

Build Log

(log yok)

Commit Diff GitHub →

editors/awk.c+13 -2
@@ -3118,9 +3118,20 @@ static var *evaluate(node *op, var *res)
case F_rn: /*rand*/
if (op1)
syntax_error("Too many arguments");
- R_d = (double)rand() / (double)RAND_MAX;
+ {
+#if RAND_MAX >= 0x7fffffff
+ uint32_t u = ((uint32_t)rand() << 16) ^ rand();
+ uint64_t v = ((uint64_t)rand() << 32) | u;
+ /* the above shift+or is optimized out on 32-bit arches */
+# if RAND_MAX > 0x7fffffff
+ v &= 0x7fffffffffffffffUL;
+# endif
+ R_d = (double)v / 0x8000000000000000UL;
+#else
+# error Not implemented for this value of RAND_MAX
+#endif
break;
-
+ }
case F_co:
if (ENABLE_FEATURE_AWK_LIBM) {
R_d = cos(L_d);