From wdunlap at tibco.com  Wed Jun  3 20:49:00 2009
From: wdunlap at tibco.com (William Dunlap)
Date: Wed, 3 Jun 2009 11:49:00 -0700
Subject: [Rd] Print bug for matrix(list(NA_complex_, ...))
In-Reply-To: <77EB52C6DD32BA4D87471DCD70C8D7000147BA06@NA-PA-VBE03.na.tibco.com>
References: <8b356f880906022219n77891154k583fecf59e96ecaf@mail.gmail.com><4A2637AD.4050801@idi.ntnu.no><18982.17460.628994.128590@lynne.math.ethz.ch>
	<77EB52C6DD32BA4D87471DCD70C8D7000147BA06@NA-PA-VBE03.na.tibco.com>
Message-ID: <77EB52C6DD32BA4D87471DCD70C8D7000147BAC4@NA-PA-VBE03.na.tibco.com>

Changing the uninitialized 'w' to 'R_print.na_width' for the complex NA
code fixes up the valgrind complaints on Linux and the bad behavior
on Windows:

===================================================================
--- print.c     (revision 48703)
+++ print.c     (working copy)
@@ -357,7 +357,7 @@
                    formatComplex(x, 1, &wr, &dr, &er, &wi, &di, &ei,
0);
                    if (ISNA(x[0].r) || ISNA(x[0].i))
                        snprintf(pbuf, 115, "%s",
-                                EncodeReal(NA_REAL, w, 0, 0, OutDec));
+                                EncodeReal(NA_REAL, R_print.na_width,
0, 0, OutDec)); /* formatReal would set w=R_print.na_width,d=0,e=0 for
NA */
                    else
                        snprintf(pbuf, 115, "%s", EncodeComplex(x[0],
                        wr, dr, er, wi, di, ei, OutDec));

E.g., the following prints go very quickly and don't have lots of
trailing
blanks.

> val <- matrix(list(NA_complex_, 3, "A string", NA_complex_), 2,2)
> print(val)
     [,1] [,2]      
[1,] NA   "A string"
[2,] 3    NA        
> print(val, na.print="<my personal missing value indicator>")
     [,1]                                 
[1,] <my personal missing value indicator>
[2,] 3                                    
     [,2]                                 
[1,] "A string"                           
[2,] <my personal missing value indicator>

Bill Dunlap
TIBCO Software Inc - Spotfire Division
wdunlap tibco.com  

> -----Original Message-----
> From: r-devel-bounces at r-project.org 
> [mailto:r-devel-bounces at r-project.org] On Behalf Of William Dunlap
> Sent: Wednesday, June 03, 2009 9:21 AM
> To: Martin Maechler; Wacek Kusnierczyk
> Cc: r-devel at r-project.org
> Subject: Re: [Rd] Print bug for matrix(list(NA_complex_, ...))
> 
> 
> > From: William Dunlap 
> > Sent: Wednesday, June 03, 2009 8:58 AM
> > To: 'Martin Maechler'; Wacek Kusnierczyk
> > Cc: r-devel at r-project.org
> > Subject: RE: [Rd] Print bug for matrix(list(NA_complex_, ...))
> ...
> > >     >> There is a bug in printing val <- 
> > > matrix(list(NA_complex_,NA_complex_),1).
> ...
> > >     >> Note that a large number of spaces are printed 
> > instead of NA.  
> > > 
> > >     vQ> on ubuntu 8.04 with r 2.10.0 r48703 there is almost 
> > > no problem (still
> > >     vQ> some unnecessary spaces):
> > > 
> > >     vQ> [,1]      [,2]    
> > >     vQ> [1,]        NA        NA
> > > 
> > > thank you, Waclav.
> > > 
> > > I don't see any unnecessary space (or other CPU problems),
> > > using another Linux variant (RHEL 5)
> > > in the three R version
> > >        	  2.9.0 ,  
> > > 	  2.9.0-patched (r48700)
> > > 	  2.10.0-devel  (r48700)
> > > 
> > > Can other Windows (or ...) users confirm problems in R 2.9.0 
> > > (or newer) ?
> > 
> > I just tried it on 2.9.0 on the Windows GUI (RGui.exe)
> > and saw similar problems.  When printing to the
> > command window, with either print(val) or show(val) it took
> > a very long time and put $'s at the ends of the lines
> > (indicating that there was text beyond the edge of the window).
> > I used sink() to put the output into a file and then print() and
> > show() acted quickly.  The resulting file had 119 character
> > lines, consisting mainly of trailing blanks:
> > 
> > > readLines("c:/temp/val.txt") # output of 
> > print(val);show(val);str(val)
> >  [1] "     [,1]                                               
> >                                                                "
> >  [2] "[1,]                                                    
> >                                                                "
> >  [3] "     [,2]                                               
> >                                                                "
> >  [4] "[1,]                                                    
> >                                                                "
> >  [5] "     [,1]                                               
> >                                                                "
> >  [6] "[1,]                                                    
> >                                                                "
> >  [7] "     [,2]                                               
> >                                                                "
> >  [8] "[1,]                                                    
> >                                                                "
> >  [9] "List of 2"                                              
> >                                                                 
> > [10] " $ : cplx NA"                                           
> >                                                                 
> > [11] " $ : cplx NA"                                           
> >                                                                 
> > [12] " - attr(*, \"dim\")= int [1:2] 1 2"                     
> >                                                                 
> > > nchar(readLines("c:/temp/val.txt"))
> >  [1] 119 119 119 119 119 119 119 119   9  12  12  32
> > 
> > On Linux, using yesterday's 2.9.0, valgrind shows lots
> > of 'depends on uninitialized value' problems that could
> > explain the Linux-Windows difference in behavior:
> > 
> > > val<-matrix(list(NA_complex_,NA_complex_),1)
> > > val
> > ==24859== Conditional jump or move depends on uninitialised value(s)
> > ==24859==    at 0x6F7E2F: vfprintf (in /lib/tls/libc-2.3.4.so)
> > ==24859==    by 0x7177A5: vsnprintf (in /lib/tls/libc-2.3.4.so)
> > ==24859==    by 0x6FF4E1: snprintf (in /lib/tls/libc-2.3.4.so)
> > ==24859==    by 0x809DDC1: Rf_EncodeReal (printutils.c:179)
> > ==24859==    by 0x8099AA9: Rf_PrintValueRec (print.c:359)
> > ==24859==    by 0x809A124: Rf_PrintValueEnv (print.c:956)
> > ==24859==    by 0x80584CA: Rf_ReplIteration (main.c:261)
> > ==24859==    by 0x8058572: R_ReplConsole (main.c:306)
> > ==24859==    by 0x8058BB8: run_Rmainloop (main.c:967)
> > ==24859==    by 0x8056A70: main (Rmain.c:33)
> 
> Here is the traceback when it enters Rf_EncodeReal:
> (gdb) where
> #0  Rf_EncodeReal (x=nan(0x0000007a2), w=168381312, d=0, e=0, cdec=46
> '.')
>     at printutils.c:160
> #1  0x08099aaa in Rf_PrintValueRec (s=0x9f4a4a8, env=0x9e4cc90) at
> print.c:359
> #2  0x0809a125 in Rf_PrintValueEnv (s=0x9f4a4a8, env=0x9e4cc90) at
> print.c:956
> #3  0x080584cb in Rf_ReplIteration (rho=0x9e4cc90, savestack=0,
> browselevel=0,
>     state=0xbffc7820) at main.c:261
> #4  0x08058573 in R_ReplConsole (rho=0x9e4cc90, savestack=0,
> browselevel=0)
>     at main.c:306
> #5  0x08058bb9 in run_Rmainloop () at main.c:967
> 
> It looks like w has a bogus value and was probably never initialized.
> It is called from print.c:
> 
>     354             case CPLXSXP:
>     355                 if (LENGTH(tmp) == 1) {
>     356                     Rcomplex *x = COMPLEX(tmp);
>     357                     formatComplex(x, 1, &wr, &dr, 
> &er, &wi, &di,
> &ei, 0);
>     358                     if (ISNA(x[0].r) || ISNA(x[0].i))
>     359                         snprintf(pbuf, 115, "%s",
>     360                                  EncodeReal(NA_REAL, w, 0, 0,
> OutDec));
>     361                     else
>     362                         snprintf(pbuf, 115, "%s",
> EncodeComplex(x[0],
>     363                         wr, dr, er, wi, di, ei, OutDec));
> 
> and I think the 'w' needs to be replaced by something reasonable for
> printing "NA"
> (or "NA_complex_?, I don't know all of what this code is used for).
> Usually complex printing uses wr and wi, not w, so w is not 
> initialized.
> 
> > ==24859==
> > ==24859== Conditional jump or move depends on uninitialised value(s)
> > ==24859==    at 0x6F7E44: vfprintf (in /lib/tls/libc-2.3.4.so)
> > ==24859==    by 0x7177A5: vsnprintf (in /lib/tls/libc-2.3.4.so)
> > ==24859==    by 0x6FF4E1: snprintf (in /lib/tls/libc-2.3.4.so)
> > ==24859==    by 0x809DDC1: Rf_EncodeReal (printutils.c:179)
> > ==24859==    by 0x8099AA9: Rf_PrintValueRec (print.c:359)
> > ==24859==    by 0x809A124: Rf_PrintValueEnv (print.c:956)
> > ==24859==    by 0x80584CA: Rf_ReplIteration (main.c:261)
> > ==24859==    by 0x8058572: R_ReplConsole (main.c:306)
> > ==24859==    by 0x8058BB8: run_Rmainloop (main.c:967)
> > ==24859==    by 0x8056A70: main (Rmain.c:33)
> > ==24859==
> > ==24859== Conditional jump or move depends on uninitialised value(s)
> > ==24859==    at 0x6F7E4C: vfprintf (in /lib/tls/libc-2.3.4.so)
> > ==24859==    by 0x7177A5: vsnprintf (in /lib/tls/libc-2.3.4.so)
> > ==24859==    by 0x6FF4E1: snprintf (in /lib/tls/libc-2.3.4.so)
> > ==24859==    by 0x809DDC1: Rf_EncodeReal (printutils.c:179)
> > ==24859==    by 0x8099AA9: Rf_PrintValueRec (print.c:359)
> > ==24859==    by 0x809A124: Rf_PrintValueEnv (print.c:956)
> > ==24859==    by 0x80584CA: Rf_ReplIteration (main.c:261)
> > ==24859==    by 0x8058572: R_ReplConsole (main.c:306)
> > ==24859==    by 0x8058BB8: run_Rmainloop (main.c:967)
> > ==24859==    by 0x8056A70: main (Rmain.c:33)
> > ==24859==
> > ==24859== Conditional jump or move depends on uninitialised value(s)
> > ==24859==    at 0x6FA87A: vfprintf (in /lib/tls/libc-2.3.4.so)
> > ==24859==    by 0x7177A5: vsnprintf (in /lib/tls/libc-2.3.4.so)
> > ==24859==    by 0x6FF4E1: snprintf (in /lib/tls/libc-2.3.4.so)
> > ==24859==    by 0x809DDC1: Rf_EncodeReal (printutils.c:179)
> > ==24859==    by 0x8099AA9: Rf_PrintValueRec (print.c:359)
> > ==24859==    by 0x809A124: Rf_PrintValueEnv (print.c:956)
> > ==24859==    by 0x80584CA: Rf_ReplIteration (main.c:261)
> > ==24859==    by 0x8058572: R_ReplConsole (main.c:306)
> > ==24859==    by 0x8058BB8: run_Rmainloop (main.c:967)
> > ==24859==    by 0x8056A70: main (Rmain.c:33)
> > ==24859==
> > ==24859== Conditional jump or move depends on uninitialised value(s)
> > ==24859==    at 0x6F86E8: vfprintf (in /lib/tls/libc-2.3.4.so)
> > ==24859==    by 0x7177A5: vsnprintf (in /lib/tls/libc-2.3.4.so)
> > ==24859==    by 0x6FF4E1: snprintf (in /lib/tls/libc-2.3.4.so)
> > ==24859==    by 0x809DDC1: Rf_EncodeReal (printutils.c:179)
> > ==24859==    by 0x8099AA9: Rf_PrintValueRec (print.c:359)
> > ==24859==    by 0x809A124: Rf_PrintValueEnv (print.c:956)
> > ==24859==    by 0x80584CA: Rf_ReplIteration (main.c:261)
> > ==24859==    by 0x8058572: R_ReplConsole (main.c:306)
> > ==24859==    by 0x8058BB8: run_Rmainloop (main.c:967)
> > ==24859==    by 0x8056A70: main (Rmain.c:33)
> > ==24859==
> > ==24859== Conditional jump or move depends on uninitialised value(s)
> > ==24859==    at 0x6F86FA: vfprintf (in /lib/tls/libc-2.3.4.so)
> > ==24859==    by 0x7177A5: vsnprintf (in /lib/tls/libc-2.3.4.so)
> > ==24859==    by 0x6FF4E1: snprintf (in /lib/tls/libc-2.3.4.so)
> > ==24859==    by 0x809DDC1: Rf_EncodeReal (printutils.c:179)
> > ==24859==    by 0x8099AA9: Rf_PrintValueRec (print.c:359)
> > ==24859==    by 0x809A124: Rf_PrintValueEnv (print.c:956)
> > ==24859==    by 0x80584CA: Rf_ReplIteration (main.c:261)
> > ==24859==    by 0x8058572: R_ReplConsole (main.c:306)
> > ==24859==    by 0x8058BB8: run_Rmainloop (main.c:967)
> > ==24859==    by 0x8056A70: main (Rmain.c:33)
> > ==24859==
> > ==24859== Conditional jump or move depends on uninitialised value(s)
> > ==24859==    at 0x711183: _IO_padn (in /lib/tls/libc-2.3.4.so)
> > ==24859==    by 0x6F8713: vfprintf (in /lib/tls/libc-2.3.4.so)
> > ==24859==    by 0x7177A5: vsnprintf (in /lib/tls/libc-2.3.4.so)
> > ==24859==    by 0x6FF4E1: snprintf (in /lib/tls/libc-2.3.4.so)
> > ==24859==    by 0x809DDC1: Rf_EncodeReal (printutils.c:179)
> > ==24859==    by 0x8099AA9: Rf_PrintValueRec (print.c:359)
> > ==24859==    by 0x809A124: Rf_PrintValueEnv (print.c:956)
> > ==24859==    by 0x80584CA: Rf_ReplIteration (main.c:261)
> > ==24859==    by 0x8058572: R_ReplConsole (main.c:306)
> > ==24859==    by 0x8058BB8: run_Rmainloop (main.c:967)
> > ==24859==    by 0x8056A70: main (Rmain.c:33)
> > ==24859==
> > ==24859== Conditional jump or move depends on uninitialised value(s)
> > ==24859==    at 0x7111BF: _IO_padn (in /lib/tls/libc-2.3.4.so)
> > ==24859==    by 0x6F8713: vfprintf (in /lib/tls/libc-2.3.4.so)
> > ==24859==    by 0x7177A5: vsnprintf (in /lib/tls/libc-2.3.4.so)
> > ==24859==    by 0x6FF4E1: snprintf (in /lib/tls/libc-2.3.4.so)
> > ==24859==    by 0x809DDC1: Rf_EncodeReal (printutils.c:179)
> > ==24859==    by 0x8099AA9: Rf_PrintValueRec (print.c:359)
> > ==24859==    by 0x809A124: Rf_PrintValueEnv (print.c:956)
> > ==24859==    by 0x80584CA: Rf_ReplIteration (main.c:261)
> > ==24859==    by 0x8058572: R_ReplConsole (main.c:306)
> > ==24859==    by 0x8058BB8: run_Rmainloop (main.c:967)
> > ==24859==    by 0x8056A70: main (Rmain.c:33)
> > Bill Dunlap
> > TIBCO Software Inc - Spotfire Division
> > wdunlap tibco.com 
> > 
> > > 
> > > Regards,
> > > Martin Maechler
> > > 
> > >     >> Compare the
> > >     >> unproblematic real case:
> > >     >> 
> > >     >> print(structure(list(NA_real_, NA_real_), .Dim = 1:2))
> > >     >> [,1] [,2]
> > >     >> [1,] NA   NA
> > >     >> 
> > >     >> Also, when printed in the read-eval-print loop, 
> > > printing takes a very very
> > >     >> long time:
> > >     >> 
> > >     >> 
> > >     >>> proc.time(); matrix(list(NA_complex_,NA_complex_),1); 
> > > proc.time()
> > >     >>> 
> > >     >> user  system elapsed
> > >     >> 74.35    0.09  329.45
> > >     >> 
> > >     >> [,1]
> > >     >> 
> > >     >> [1,]
> > >     >> 
> > >     >> 
> > >     >> [,2]
> > >     >> 
> > >     >> [1,]
> > >     >> 
> > >     >> user  system elapsed
> > >     >> 92.63    0.15  347.86
> > >     >> 
> > >     >> 18 seconds runtime!
> > >     >> 
> > > 
> > >     vQ> user  system elapsed
> > >     vQ> 0.648   0.056 155.843
> > >     vQ> [,1] [,2]
> > >     vQ> [1,]  NA   NA
> > >     vQ> user  system elapsed
> > >     vQ> 0.648   0.056 155.843
> > > 
> > >     vQ> vQ
> > > 
> > >     vQ> ______________________________________________
> > >     vQ> R-devel at r-project.org mailing list
> > >     vQ> https://stat.ethz.ch/mailman/listinfo/r-devel
> > > 
> > > ______________________________________________
> > > R-devel at r-project.org mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-devel
> > > 
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 


[Rd] Print bug for matrix(list(NA_complex_, ...))
William Dunlap