www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - std.stream.MemoryStream.writeBlock bug.

reply Hiroshi Sakurai <Hiroshi_member pathlink.com> writes:
std.stream.MemoryStream.writeBlock bug.

The size beyond the length of array is specified.
Even the data of domains other than array will write out.

source code

import std.stream;

void main() {
MemoryStream ms = new MemoryStream();
byte[] b;
b.length = 10;
for (int i = 0; i < b.length; i++) {
b[i] = i;
}
ms.writeBlock(b, 11);
printf("length = %d\n", ms.data.length);
}

output

11
Dec 12 2004
parent reply Ben Hinkle <bhinkle4 juno.com> writes:
Hiroshi Sakurai wrote:

 std.stream.MemoryStream.writeBlock bug.
 
 The size beyond the length of array is specified.
 Even the data of domains other than array will write out.
 
 source code
 
 import std.stream;
 
 void main() {
 MemoryStream ms = new MemoryStream();
 byte[] b;
 b.length = 10;
 for (int i = 0; i < b.length; i++) {
 b[i] = i;
 }
 ms.writeBlock(b, 11);
 printf("length = %d\n", ms.data.length);
 }
 
 output
 
 11
I don't think that is a bug. The "writeBlock" function takes a void* buffer pointer and a length, so the fact that the buffer originally came from an array is forgotten inside writeBlock. To write out exactly a buffer try "write(ubyte[] buf)". I suppose you'll have to cast the buffer since byte[] isn't implicitly convertable to ubyte[]. In other words instead of ms.writeBlock(b,11) try ms.write(b); -Ben
Dec 12 2004
parent Hiroshi Sakurai <Hiroshi_member pathlink.com> writes:
In article <cpipj0$6jv$1 digitaldaemon.com>, Ben Hinkle says...
Hiroshi Sakurai wrote:

 std.stream.MemoryStream.writeBlock bug.
 
 The size beyond the length of array is specified.
 Even the data of domains other than array will write out.
 
 source code
 
 import std.stream;
 
 void main() {
 MemoryStream ms = new MemoryStream();
 byte[] b;
 b.length = 10;
 for (int i = 0; i < b.length; i++) {
 b[i] = i;
 }
 ms.writeBlock(b, 11);
 printf("length = %d\n", ms.data.length);
 }
 
 output
 
 11
I don't think that is a bug. The "writeBlock" function takes a void* buffer pointer and a length, so the fact that the buffer originally came from an array is forgotten inside writeBlock. To write out exactly a buffer try "write(ubyte[] buf)". I suppose you'll have to cast the buffer since byte[] isn't implicitly convertable to ubyte[]. In other words instead of ms.writeBlock(b,11) try ms.write(b); -Ben
Thank you for a reply. Ben. Well. I mistook about Phobos spec. -Hiroshi Sakurai
Dec 13 2004