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 diff --git a/src/gsumm.c b/src/gsumm.c index 372ae59440..ed34e76207 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(nBatch, false)) // schedule(dynamic,1) for (int b=0; b