Skip to content
Prev 41333 / 63421 Next

cbind() crashes on raw vectors

On 09/23/2011 01:46 PM, Herv? Pag?s wrote:
For what it's worth, under valgrind

R -d valgrind -e "df <- cbind(a=integer(4000), b=raw(4000)) "
==19446== Memcheck, a memory error detector
==19446== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==19446== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==19446== Command: /home/mtmorgan/bin/R-devel/bin/exec/R --no-save 
--quiet -e df~+~\<-~+~cbind(a=integer(4000),~+~b=raw(4000))~+~
==19446==
 > df <- cbind(a=integer(4000), b=raw(4000))
==19446== Invalid write of size 8
==19446==    at 0x4E8C488: cbind (bind.c:1318)
==19446==    by 0x4E8B910: do_bind (bind.c:1117)
==19446==    by 0x4F77460: do_internal (names.c:1229)
==19446==    by 0x4F14543: Rf_eval (eval.c:469)
==19446==    by 0x4F154EC: Rf_applyClosure (eval.c:838)
==19446==    by 0x4F14856: Rf_eval (eval.c:513)
==19446==    by 0x4F17F19: do_set (eval.c:1724)
==19446==    by 0x4F14543: Rf_eval (eval.c:469)
==19446==    by 0x4F5D0A8: Rf_ReplIteration (main.c:255)
==19446==    by 0x4F5D295: R_ReplConsole (main.c:304)
==19446==    by 0x4F5E8F4: run_Rmainloop (main.c:985)
==19446==    by 0x4F5E909: Rf_mainloop (main.c:992)
==19446==  Address 0x8253298 is 0 bytes after a block of size 32,040 alloc'd
==19446==    at 0x4C26C3A: malloc (in 
/usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)

and at bind.c:1306:1319

		if (TYPEOF(u) <= INTSXP) {
		    if (mode <= INTSXP) {
			for (i = 0; i < idx; i++)
			    INTEGER(result)[n++] = INTEGER(u)[i % k];
		    }
		    else {
			for (i = 0; i < idx; i++)
			    REAL(result)[n++] = (INTEGER(u)[i % k]) == NA_INTEGER ? NA_REAL : 
INTEGER(u)[i % k];
		    }
		}
		else {
		    for (i = 0; i < idx; i++)
			REAL(result)[n++] = REAL(u)[i % k];
		}

we end up writing REAL(result)[n++] = REAL(u)[i % k] even though

  (gdb) p TYPEOF(u)
$15 = 24

i.e., RAWSXP

(gdb) p TYPEOF(result)
$16 = 13

i.e., INTSXP

it's because the test TYPEOF(u) <= INTSXP fails bind.c:1306.

Martin

  sessionInfo()
R Under development (unstable) (2011-09-19 r57030)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
  [7] LC_PAPER=C                 LC_NAME=C
  [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base