You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@systemds.apache.org by mb...@apache.org on 2021/07/25 16:28:47 UTC

[systemds] 01/02: [SYSTEMDS-3072] Fix integer overflow in sparse random number generation

This is an automated email from the ASF dual-hosted git repository.

mboehm7 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/systemds.git

commit 698388ed12ae99ba57e4952331a398544df72e0f
Author: Matthias Boehm <mb...@gmail.com>
AuthorDate: Sun Jul 25 17:40:53 2021 +0200

    [SYSTEMDS-3072] Fix integer overflow in sparse random number generation
    
    This patch fixes an integer overflow in rand(), which has specialized
    code paths for dense and spare random matrix generation. For sparse, we
    compute skips until the next value, and on a scenario with ultra-sparse
    matrices (with sparsity 1e-11) this skip ran into an integer overflow.
---
 .../apache/sysds/runtime/matrix/data/LibMatrixDatagen.java   | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixDatagen.java b/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixDatagen.java
index 586dc5a..b547b4f 100644
--- a/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixDatagen.java
+++ b/src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixDatagen.java
@@ -533,15 +533,17 @@ public class LibMatrixDatagen
 	{
 		// Prob [k-1 zeros before a nonzero] = Prob [k-1 < log(uniform)/log(1-p) < k] = p*(1-p)^(k-1), where p=sparsity
 		double log1mp = Math.log(1-sparsity);
-		int idx = 0;  // takes values in range [1, blen*blen] (both ends including)
+		long idx = 0;  // takes values in range [1, blen*blen] (both ends including)
 		long blocksize = blockrows*blockcols;
 		while(idx < blocksize) {
 			//compute skip to next index
-			idx = idx + (int) Math.ceil(Math.log(nnzPRNG.nextDouble())/log1mp);
-			if ( idx > blocksize) break;
+			idx = idx + (long) Math.ceil(Math.log(nnzPRNG.nextDouble())/log1mp);
+			//check blocksize and save int casts
+			if ( idx > blocksize)
+				break;
 			// translate idx into (r,c) within the block
-			int rix = (idx-1)/blockcols;
-			int cix = (idx-1)%blockcols;
+			int rix = (int)(idx-1)/blockcols;
+			int cix = (int)(idx-1)%blockcols;
 			double val = min + (range * valuePRNG.nextDouble());
 			c.allocate(rowoffset+rix, estnnzRow, clen);
 			c.append(rowoffset+rix, coloffset+cix, val);