joshuaulrich / TTR

Technical analysis and other functions to construct technical trading rules with R
GNU General Public License v2.0
325 stars 102 forks source link

segfault in DVI caused by runPercentRank #112

Closed ifellows closed 2 years ago

ifellows commented 3 years ago

Description

I am getting segfaults from the DVI function

Minimal, reproducible example

df <- structure(list(c = c(20.7, 20.7, 20, 20.95, 20.4, 20.71, 20.65, 
20.38, 21.38, 22.9, 24.95, 22.8, 22.45, 22.83, 22.25, 22.22, 
22.21, 22.2, 22.14, 22.8, 23.92, 22.65, 22.65, 22.6, 22.5, 22.55, 
22.65, 22.9, 22.5, 23.3, 22.89, 23.11, 22.45, 23.1, 23.33, 23.69, 
23.48, 23.2, 23.34, 23.34, 23.64, 23.79, 23.95, 23.7, 24.15, 
24.49, 24.87, 25.02, 23.92, 23.71, 23.36, 22.97, 22.7, 22.5, 
22.75, 23.16, 23.07, 23.53, 24.99, 24.53, 24.47, 24.68, 21.93, 
21.91, 21.91, 21.51, 20.7, 20.53, 19.51, 19.65, 20, 20.03, 20.35, 
20.53, 20.64, 20.7, 20.66, 20.5, 20.57, 20.61, 20.6, 20.71, 19.99, 
19.46, 18.56, 20.39, 20.76, 20.74, 20.67, 20.49, 20.47, 20.08, 
19.39, 20.51, 20.65, 20.65, 19.99, 19.86, 19.86, 19.51, 19.74, 
19.46, 18.88, 18.84, 19.25, 19.08, 19.43, 19.39, 19.02, 19.06, 
19.03, 19.05, 19.11, 19, 19.32, 19, 19, 18.76, 18.18, 18, 17.58, 
16, 15.9, 16, 15.97, 15.98, 16.23, 16.6, 16.9, 17.02, 17, 17.15, 
17.38, 17.6, 18.2, 18, 17.02, 16.99, 16.69, 17.11, 17.3, 17.74, 
17.69, 17.99, 18.07, 18.19, 18.98, 19.24, 19.8, 19.72, 19.04, 
18.34, 17.7, 17.5, 17.31, 17.67, 17.89, 18.48, 18.24, 18, 18.04, 
18.24, 18.1, 17.89, 17.92, 16.57, 16.47, 17.95, 16.98, 16.75, 
16.79, 16.66, 17.65, 17.92, 17.51, 17.7, 17.71, 17.01, 16.22, 
15.96, 15.91, 15.74, 16.53, 16.47, 16.18, 16, 16.14, 16.38, 16.59, 
16.24, 17.29, 16.02, 16.5, 16.5, 16.01, 16.16, 16.11, 15.91, 
15.44, 15.99, 15.9, 15.94, 15.72, 16.02, 16.11, 16.95, 16.55, 
17.2, 17.42, 17.36, 17.31, 17.03, 16.65, 16.77, 16.99, 16.51, 
17, 17.1, 16.91, 16.47, 16.2, 15.47, 16.95, 17.01, 16.65, 16.28, 
16.25, 16.03, 16.06, 16.17, 16.95, 16.4, 16.91, 17.5, 17, 17.22, 
17.56, 18.5, 19, 18.29, 18.94, 19.5, 19.69, 19.4, 19.34, 19.69, 
19.58, 19.23, 19.15, 19.1, 18.59, 18.01, 18, 18.2, 18.12, 18.92, 
19.35, 19.18, 19.05, 18.99, 19.55, 19.05, 18.95, 18.31, 19.31, 
18.3, 19.25, 19.1, 18.78, 18.82, 18.76, 18.75, 18.8, 18.82, 18.35, 
18.72, 18.65, 18.55, 18.47, 18.73, 18.7, 18.7, 18.65, 18.58)), row.names = c(NA, 
-284L), class = "data.frame")

DVI(df[c("c")])

Session Info

R version 3.6.2 (2019-12-12)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 10 (buster)

Matrix products: default
BLAS/LAPACK: /usr/lib/x86_64-linux-gnu/libopenblasp-r0.3.5.so

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=C             
 [7] LC_PAPER=en_US.UTF-8       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     

other attached packages:
[1] TTR_0.24.2

loaded via a namespace (and not attached):
[1] zoo_1.8-8       compiler_3.6.2  tools_3.6.2     xts_0.12.1     
[5] curl_4.3        grid_3.6.2      lattice_0.20-38
joshuaulrich commented 3 years ago

Thanks for the report. I get a malloc() error and core dump.

R$ DVI(df[c("c")])
malloc(): corrupted top size
Aborted (core dumped)

Is that what you get?

ifellows commented 3 years ago

Yup. Tracked it down to the C code called from runPercentRank, but didn't get any further.

joshuaulrich commented 3 years ago

The problem is that df has 284 rows, but DVI needs at least 353 observations to calculate one result with the default argumen values. It uses 100 observations for magnitude and stretch and then 252 observations for n. So it adds 100 NA at the beginning of your series, which only leaves 184 observations for the runPercentRank() calculation.

All that said, it shouldn't crash your R session. The same bug exists in the C code for other functions (e.g. wilderSum()). But those don't crash because the R code checks for valid values for n given the number of leading NA. I'll work on a patch for all affected functions.