www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - I hate ".dup"

reply Qian Xu <quian.xu stud.tu-ilmenau.de> writes:
I have spent one day to find out an error.

-----------------------------------------------------------------
class MyTime
{
  this(char[] timestring)
  {
     Time t;
     int p = tango.time.TimeStamp.iso8601(timestring, t);
     //...
  }
  // other methods
}
unittest
{
  MyTime mt = new MyTime("01:10:20,050");
  assert(MyTime.addMillis(mt, 1).toString() == "01:10:20,051"); // sometimes
failed
}
-----------------------------------------------------------------

I ran this unit-test together with some other tests. Sometimes no error,
sometimes with errors. 

Finally I have added timestring.dup in constructor, the problem does not
appear any more. 

Oh god. I have to add ".dup" at the end of every string to avoid potential
program errors. This is so incredible....


Best regards
Qian Xu
Mar 23 2009
next sibling parent reply BCS <none anon.com> writes:
Hello Qian,

 Oh god. I have to add ".dup" at the end of every string to avoid
 potential program errors. This is so incredible....
 
Run it on linux and it will seg-v when you try to access a literal string. Oh, and that issue is in no way unique to D. Any language with mutable strings will have it and any language without will force the copies anyway (but with better syntax)
Mar 23 2009
parent Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Mon, Mar 23, 2009 at 12:15 PM, BCS <none anon.com> wrote:
 Hello Qian,

 Oh god. I have to add ".dup" at the end of every string to avoid
 potential program errors. This is so incredible....
Run it on linux and it will seg-v when you try to access a literal string. Oh, and that issue is in no way unique to D. Any language with mutable strings will have it and any language without will force the copies anyway (but with better syntax)
It is unique to DMD/Optlink. GDC Win will produce EXEs that throw a segfault if you attempt to modify a read-only string.
Mar 23 2009
prev sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 23 Mar 2009 11:07:16 -0400, Qian Xu <quian.xu stud.tu-ilmenau.de>
wrote:

 I have spent one day to find out an error.

 -----------------------------------------------------------------
 class MyTime
 {
   this(char[] timestring)
   {
      Time t;
      int p = tango.time.TimeStamp.iso8601(timestring, t);
      //...
   }
   // other methods
 }
 unittest
 {
   MyTime mt = new MyTime("01:10:20,050");
   assert(MyTime.addMillis(mt, 1).toString() == "01:10:20,051"); //  
 sometimes
 failed
 }
 -----------------------------------------------------------------

 I ran this unit-test together with some other tests. Sometimes no error,
 sometimes with errors.

 Finally I have added timestring.dup in constructor, the problem does not
 appear any more.

 Oh god. I have to add ".dup" at the end of every string to avoid  
 potential
 program errors. This is so incredible....


 Best regards
 Qian Xu
Coincidentally, someone else discovered a buffer-overrun bug in tango.text.convert.TimeStamp, see http://www.dsource.org/projects/tango/forums/topic/704 Please try downloading the latest trunk code and see if your code still fails. You should not have to .dup the string, if you do, that is a bug. -Steve
Mar 23 2009
parent reply Qian Xu <quian.xu stud.tu-ilmenau.de> writes:
Steven Schveighoffer wrote:
 
 Coincidentally, someone else discovered a buffer-overrun bug in
 tango.text.convert.TimeStamp, see
 http://www.dsource.org/projects/tango/forums/topic/704
 
 Please try downloading the latest trunk code and see if your code still
 fails.
 
 You should not have to .dup the string, if you do, that is a bug.
 
 -Steve
TimeStamp.iso8601(..) does not call the parse() function.
Mar 24 2009
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 24 Mar 2009 12:26:29 -0400, Qian Xu <quian.xu stud.tu-ilmenau.de>  
wrote:

 Steven Schveighoffer wrote:
 Coincidentally, someone else discovered a buffer-overrun bug in
 tango.text.convert.TimeStamp, see
 http://www.dsource.org/projects/tango/forums/topic/704

 Please try downloading the latest trunk code and see if your code still
 fails.

 You should not have to .dup the string, if you do, that is a bug.

 -Steve
TimeStamp.iso8601(..) does not call the parse() function.
The bug was in parseInt. Please try it. -Steve
Mar 24 2009
parent Qian Xu <quian.xu stud.tu-ilmenau.de> writes:
Steven Schveighoffer wrote:
 TimeStamp.iso8601(..) does not call the parse() function.
The bug was in parseInt. Please try it.
It worth a try. I have added too many ".dup" in my code. I will confirm this ASAP. Thanks for your information ^^) -- Xu, Qian (stanleyxu) http://stanleyxu2005.blogspot.com
Mar 24 2009