From 537710f07b4e5a722b82f4ba31968ad1cf2fe42a Mon Sep 17 00:00:00 2001 From: shrektan Date: Wed, 11 Mar 2020 21:19:39 +0800 Subject: [PATCH 1/2] should use the long int 2 in case of integer overflow --- src/gsumm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gsumm.c b/src/gsumm.c index ef63519a3c..8dccb736b3 100644 --- a/src/gsumm.c +++ b/src/gsumm.c @@ -112,7 +112,7 @@ SEXP gforce(SEXP env, SEXP jsub, SEXP o, SEXP f, SEXP l, SEXP irowsArg) { int highSize = ((nrow-1)>>shift) + 1; //Rprintf(_("When assigning grp[o] = g, highSize=%d nb=%d shift=%d nBatch=%d\n"), highSize, nb, shift, nBatch); int *counts = calloc(nBatch*highSize, sizeof(int)); // TODO: cache-line align and make highSize a multiple of 64 - int *TMP = malloc(nrow*2*sizeof(int)); + int *TMP = malloc(nrow*2l*sizeof(int)); // must multiple the long int otherwise overflow may happen, #4295 if (!counts || !TMP ) error(_("Internal error: Failed to allocate counts or TMP when assigning g in gforce")); #pragma omp parallel for num_threads(getDTthreads()) // schedule(dynamic,1) for (int b=0; b Date: Thu, 10 Dec 2020 00:11:02 +0800 Subject: [PATCH 2/2] update NEWS --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 472cc52dca..bc5a49b639 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,6 +6,8 @@ ## BUG FIXES +1. `gforce()` now allocates the correct amount of memory for the data.table with more than 1e9 rows, [#4295](https://github.com/Rdatatable/data.table/issues/4295) and [#4818](https://github.com/Rdatatable/data.table/issues/4818). Before the fixing, data.table could throw an error "Failed to allocate counts or TMP when assigning g in gforce", due to an integer overflow when `malloc()` memories. Thanks to @renkun-ken and @jangorecki for reporting and @shrektan for fixing. + ## NOTES