Bug 16685 - format.POSIXlt crashes with modified $zone, multiple 'format' strings
Summary: format.POSIXlt crashes with modified $zone, multiple 'format' strings
Status: CLOSED FIXED
Alias: None
Product: R
Classification: Unclassified
Component: Misc (show other bugs)
Version: R-devel (trunk)
Hardware: x86_64/x64/amd64 (64-bit) Linux
: P5 major
Assignee: R-core
URL:
Depends on:
Blocks:
 
Reported: 2016-01-28 10:01 UTC by Mikko Korpela
Modified: 2016-05-17 08:29 UTC (History)
2 users (show)

See Also:


Attachments
Proposed patch (1.48 KB, patch)
2016-01-28 10:01 UTC, Mikko Korpela
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Mikko Korpela 2016-01-28 10:01:18 UTC
Created attachment 2011 [details]
Proposed patch

When the zone component of a POSIXlt object is shortened, format.POSIXlt crashes the R process. Example code and output:

  > foo <- rep(as.POSIXlt("2016-01-28 01:23:45", tz = "Europe/Stockholm"), 2)
  > foo
  [1] "2016-01-28 01:23:45 CET" "2016-01-28 01:23:45 CET"
  > foo$zone <- foo$zone[1]
  > foo
  
   *** caught segfault ***
  address 0x20000035, cause 'memory not mapped'

  Traceback:
   1: format.POSIXlt(x, usetz = TRUE)
   2: format(x, usetz = TRUE)
   3: print(format(x, usetz = TRUE), ...)
   4: print.POSIXlt(x)
   5: function (x, ...) UseMethod("print")(x)
  
  Possible actions:
  1: abort (with core dump, if enabled)
  2: normal R exit
  3: exit R without saving workspace
  4: exit R saving workspace
  Selection: 

Another crash, when 'format' contains more items than the date-time input 'x':

  > foo <- as.POSIXlt("2016-01-28 01:23:45", tz = "Europe/Stockholm")
  > format(foo, format=c("%d", "%y"))
  
   *** caught segfault ***
  address 0x210000b0, cause 'memory not mapped'
  
  Traceback:
   1: format.POSIXlt(foo, format = c("%d", "%y"))
   2: format(foo, format = c("%d", "%y"))
  
  Possible actions:
  1: abort (with core dump, if enabled)
  2: normal R exit
  3: exit R without saving workspace
  4: exit R saving workspace
  Selection: 3
  Warning message:
  In if (format == "") { :
    the condition has length > 1 and only the first element will be used
  
Yes, the user is probably not supposed to modify $zone like this. Also, in my opinion, ?format.POSIXlt gives mixed messages about the support for multiple items in the 'format' vector. The crashes were observed on R-devel r70020 (Linux and Mac) and on R 3.2.3 patched r70021 (Linux).

I have a few suggested modifications to src/main/datetime.c (see the attached file). With these applied, the output is as follows:

  > foo <- rep(as.POSIXlt("2016-01-28 01:23:45", tz = "Europe/Stockholm"), 2)
  > foo
  [1] "2016-01-28 01:23:45 CET" "2016-01-28 01:23:45 CET"
  > foo$zone <- foo$zone[1]
  > foo
  [1] "2016-01-28 01:23:45 Europe/Stockholm"
  [2] "2016-01-28 01:23:45 Europe/Stockholm"
  > foo <- as.POSIXlt("2016-01-28 01:23:45", tz = "Europe/Stockholm")
  > format(foo, format=c("%d", "%y"))
  [1] "28" "16"
  Warning message:
  In if (format == "") { :
    the condition has length > 1 and only the first element will be used

Details about one of the crashed R sessions:

  > sessionInfo()
  R Under development (unstable) (2016-01-27 r70020)
  Platform: x86_64-pc-linux-gnu (64-bit)
  Running under: Ubuntu 14.04.3 LTS
  
  locale:
   [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
   [3] LC_TIME=fi_FI.UTF-8        LC_COLLATE=en_US.UTF-8    
   [5] LC_MONETARY=fi_FI.UTF-8    LC_MESSAGES=en_US.UTF-8   
   [7] LC_PAPER=fi_FI.UTF-8       LC_NAME=C                 
   [9] LC_ADDRESS=C               LC_TELEPHONE=C            
  [11] LC_MEASUREMENT=fi_FI.UTF-8 LC_IDENTIFICATION=C       
  
  attached base packages:
  [1] stats     graphics  grDevices utils     datasets  methods   base
Comment 1 Martin Maechler 2016-01-28 16:53:17 UTC
bug *and* bug fix confirmed -->  Thank you!

I'm currently looking at the  warning you've reported which is most probably also "sub optimal".
Comment 2 Martin Maechler 2016-01-29 18:11:50 UTC
(In reply to Martin Maechler from comment #1)
> bug *and* bug fix confirmed -->  Thank you!
> 
> I'm currently looking at the  warning you've reported which is most probably
> also "sub optimal".

and that has detected problems -- now fixed -- in determining default 'format' when format is *partially* specified (some "" some "<format").

I've committed a fix to R-devel and R-patched, now.
Comment 3 Martin Maechler 2016-05-17 08:29:49 UTC
*** Bug 16909 has been marked as a duplicate of this bug. ***