www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - druntime giving wrong line for copy assert?

reply Rudy Raab <TransientResponse outlook.com> writes:
I didn't want to immediately declare this a compiler/runtime bug 
without making sure I wasn't crazy/misinterpreting things.

Backstory is this: I was writing a library for dealing with some 
special corporate data to (de)serialize to/from a binary format 
and tweaking things to get the output to match a known good 

from binary -> class instance -> back to binary and ensure input 
and output match.

At one point I got an assert failure in an odd place; it seemed 
to be coming from inside std.format.format. So I wrote a simple 
test program to recreate the issue:

---

/+ dub.sdl:
	name "hello"
+/

import std.stdio;
import std.datetime;
import std.regex;
import std.base64;
import std.string;
import std.algorithm;

private immutable string[] months = ["Jan", "Feb", "Mar", "Apr", 
"May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

void main()
{
     string dstr = "29-Apr-2019 11:04";
     SysTime dateProgrammed;

     ubyte[] full = new ubyte[128];

     auto m = matchFirst(dstr, 
regex(r"([0-9]{2})-([A-Z][a-z]{2})-([0-9]{4}) 
([0-9]{2}):([0-9]{2})"));
     assert(m.length > 0, "Date string is invalid");
     import std.conv: to;
     DateTime d = DateTime(1990, 1, 1);
     d.month = to!Month(months.join.indexOf(m[2])/3+1);
     d.year = m[3].to!short;
     d.day = m[1].to!ubyte;
     d.hour = m[4].to!ubyte;
     d.minute = m[5].to!ubyte;
     dateProgrammed = SysTime(d, 0.seconds);

     copy("test_string".representation, full[8..16]);

     import std.format: format;
     string date = format!("%d-%3s-%d 
%02d:%02d")(dateProgrammed.day,
         months[dateProgrammed.month-1], dateProgrammed.year,
     dateProgrammed.hour, dateProgrammed.minute);
     copy(date.representation, full[24..44]);

     Base64.encode(full).writeln;
}
---

The problem is on line 33, the copy of "test_string". The slice 
to copy it into is too short. But when I run it with dub, the 
output is this:
---
core.exception.AssertError C:\D\dmd2\windows\bin\..\..\src\phobos\std\algori
hm\mutation.d(373): Cannot copy a source range into a smaller target range.
----------------
0x0043BE49 in _d_assert_msg
0x0040252B in _Dmain at <snip>\test_format_assert.d(36)
0x0043B96B in void rt.dmain2._d_run_main(int, char**, extern (C) 
int function(char[][])*).runAll().__lambda1()
0x0043B8ED in void rt.dmain2._d_run_main(int, char**, extern (C) 
int function(char[][])*).runAll()
0x0043B787 in _d_run_main
0x0043B518 in main at <snip>\test_format_assert.d(7)
0x0048CED5 in mainCRTStartup
0x767A0419 in BaseThreadInitThunk
0x7730662D in RtlGetAppContainerNamedObjectPath
0x773065FD in RtlGetAppContainerNamedObjectPath
---

The error was on line 36, which is the format, not my bad slice. 
Is there a reason for this, or is it a bug somewhere?
Apr 30 2019
parent reply Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Tuesday, 30 April 2019 at 13:59:52 UTC, Rudy Raab wrote:
 The error was on line 36, which is the format, not my bad 
 slice. Is there a reason for this, or is it a bug
The message is correct when not using dub: https://run.dlang.io/is/chFTOY I suspect that if you remove the dub related lines and put it in an empty dub project, it will produce a correct message. So it looks like a bug in dub from where I am sitting. Maybe dub adds lines behind the scene? Bastiaan.
Apr 30 2019
parent reply Rudy Raab <TransientResponse outlook.com> writes:
On Tuesday, 30 April 2019 at 20:35:11 UTC, Bastiaan Veelo wrote:
 On Tuesday, 30 April 2019 at 13:59:52 UTC, Rudy Raab wrote:
 The error was on line 36, which is the format, not my bad 
 slice. Is there a reason for this, or is it a bug
The message is correct when not using dub: https://run.dlang.io/is/chFTOY I suspect that if you remove the dub related lines and put it in an empty dub project, it will produce a correct message. So it looks like a bug in dub from where I am sitting. Maybe dub adds lines behind the scene? Bastiaan.
I had it as a single file dub project because it was a distilled version of an example from a larger dub project I was working on, and I wanted to keep everything as similar as possible. But you are correct, the incorrect error line only appears when it's built with dub. I'll go looking at dub bugs and see if it hasn't been reported yet.
May 01 2019
parent Rudy Raab <TransientResponse outlook.com> writes:
On Wednesday, 1 May 2019 at 12:29:18 UTC, Rudy Raab wrote:
 On Tuesday, 30 April 2019 at 20:35:11 UTC, Bastiaan Veelo wrote:
 On Tuesday, 30 April 2019 at 13:59:52 UTC, Rudy Raab wrote:
 The error was on line 36, which is the format, not my bad 
 slice. Is there a reason for this, or is it a bug
The message is correct when not using dub: https://run.dlang.io/is/chFTOY I suspect that if you remove the dub related lines and put it in an empty dub project, it will produce a correct message. So it looks like a bug in dub from where I am sitting. Maybe dub adds lines behind the scene? Bastiaan.
I had it as a single file dub project because it was a distilled version of an example from a larger dub project I was working on, and I wanted to keep everything as similar as possible. But you are correct, the incorrect error line only appears when it's built with dub. I'll go looking at dub bugs and see if it hasn't been reported yet.
As it turns out a simple test case compiled with DMD with the -g flag has the issue, regardless of dub. It also doesn't appear on Linux or with LDC on either platform, so it looks like a Windows-specific DMD issue.
May 02 2019