www.digitalmars.com         C & C++   DMDScript  

D - bug? char or byte arrays that are 32 long

reply nick.fletcher dingoblue.com.au writes:
hi, first time posting here, so go easy if ive done something wrong :)

ive written a password generator in D.  nice language :)
the problem occurs whenever i output (printf) a passwords that are a multiple of
32 characters long.

it will print out the password, then concatenate the previous password onto it
as well.  eg:

C:\D\myPrograms\passGen>passgen -iterate 10 -min 25 -max 32 -notsayable
VmJmQMCGalHJmLGkyZCqiKTBneOe
oXOYHujRqiWIiPjawrQmNBFiCxVPdhhKVmJmQMCGalHJmLGkyZCqiKTBneOe
MCeGuhbIjILHGUjZIAPtwXCTa
YvlPqNtmDMoSaCRvYePlRKhUJBO
cRodRplUJmbnYOeJPDuAJdbfKmcbQ
SQLSRdARdthOHuNOTAEZLAMnQTB
IHZdqDLivShuhxEvckBprZFtmHZWQ
PMBdFYydCrSFxpioxefBQdUTaWVOB
bpEVXTVyoMsBgkWnbYywIwhqr
sXjQybaGGPnPvuvWuODASLsUgLKVq

if you look closely, you can see the second password is obviously too long, and
the last 28 characters are actually the previous password

if i tell it to print out the length, it never says 64 or whatever, it still
pretends the password is just 32.

i can attatch my code if you want, but its about 600 lines long.

basically i get a random character, and keep appending random characters to it
until its the length it is supposed to be.  i then printf the password, and if
its 32 chars long, it will either print out the previous password as a

end of the password.

im quite stumped.

if i only tell it to print out a single generated password, it does this:

C:\D\myPrograms\passGen>passgen -iterate 1 -min 32 -max 32 -notsayable
sgJJeNKrfycrhSdYBKPUvEEubCKFswNMThis    is       a fofof        This is a silly
silly list


i can assure you i dont even have the word "silly" anywhere in my code :)

thanks for listening, i definitely dont pretend to be a great programmer, so
feel free to slap me if there's some programming standard i dont know about that
does this.

Nick.
Feb 06 2003
parent reply "Mike Wynn" <mike.wynn l8night.co.uk> writes:
if you posted some code it might help, perhaps you can produce a 10 lines
prog that exibits the same behaviour

this is just a guess
you use printf( "%s", cast(char *)str )  ?
try
printf( "%.*s", str )

D strings (char[]) are slices into arrays i.e. struct{pointer to first char,
length} not null termed strings like C

Mike.

<nick.fletcher dingoblue.com.au> wrote in message
news:b1v0ke$2dit$1 digitaldaemon.com...
 hi, first time posting here, so go easy if ive done something wrong :)

 ive written a password generator in D.  nice language :)
 the problem occurs whenever i output (printf) a passwords that are a
multiple of
 32 characters long.

 it will print out the password, then concatenate the previous password
onto it
 as well.  eg:

 C:\D\myPrograms\passGen>passgen -iterate 10 -min 25 -max 32 -notsayable
 VmJmQMCGalHJmLGkyZCqiKTBneOe
 oXOYHujRqiWIiPjawrQmNBFiCxVPdhhKVmJmQMCGalHJmLGkyZCqiKTBneOe
 MCeGuhbIjILHGUjZIAPtwXCTa
 YvlPqNtmDMoSaCRvYePlRKhUJBO
 cRodRplUJmbnYOeJPDuAJdbfKmcbQ
 SQLSRdARdthOHuNOTAEZLAMnQTB
 IHZdqDLivShuhxEvckBprZFtmHZWQ
 PMBdFYydCrSFxpioxefBQdUTaWVOB
 bpEVXTVyoMsBgkWnbYywIwhqr
 sXjQybaGGPnPvuvWuODASLsUgLKVq

 if you look closely, you can see the second password is obviously too
long, and
 the last 28 characters are actually the previous password

 if i tell it to print out the length, it never says 64 or whatever, it
still
 pretends the password is just 32.

 i can attatch my code if you want, but its about 600 lines long.

 basically i get a random character, and keep appending random characters
to it
 until its the length it is supposed to be.  i then printf the password,
and if
 its 32 chars long, it will either print out the previous password as a

to the
 end of the password.

 im quite stumped.

 if i only tell it to print out a single generated password, it does this:

 C:\D\myPrograms\passGen>passgen -iterate 1 -min 32 -max 32 -notsayable
 sgJJeNKrfycrhSdYBKPUvEEubCKFswNMThis    is       a fofof        This is a
silly
 silly list


 i can assure you i dont even have the word "silly" anywhere in my code :)

 thanks for listening, i definitely dont pretend to be a great programmer,
so
 feel free to slap me if there's some programming standard i dont know
about that
 does this.

 Nick.
Feb 06 2003
parent reply nick.fletcher dingoblue.com.au writes:
that worked, thanks!

could i just ask, whats the difference between

printf("%s", (char *)str[]);   //   <-- this is what i used

and

printf("%.*s", str);    //   <-- this is what worked

?
from what you say, using the first one ignore the length property of the char
array and prints till it can print no more?



In article <b1v1bi$2due$1 digitaldaemon.com>, Mike Wynn says...
if you posted some code it might help, perhaps you can produce a 10 lines
prog that exibits the same behaviour

this is just a guess
you use printf( "%s", cast(char *)str )  ?
try
printf( "%.*s", str )

D strings (char[]) are slices into arrays i.e. struct{pointer to first char,
length} not null termed strings like C

Mike.

<nick.fletcher dingoblue.com.au> wrote in message
news:b1v0ke$2dit$1 digitaldaemon.com...
 hi, first time posting here, so go easy if ive done something wrong :)

 ive written a password generator in D.  nice language :)
 the problem occurs whenever i output (printf) a passwords that are a
multiple of
 32 characters long.

 it will print out the password, then concatenate the previous password
onto it
 as well.  eg:

 C:\D\myPrograms\passGen>passgen -iterate 10 -min 25 -max 32 -notsayable
 VmJmQMCGalHJmLGkyZCqiKTBneOe
 oXOYHujRqiWIiPjawrQmNBFiCxVPdhhKVmJmQMCGalHJmLGkyZCqiKTBneOe
 MCeGuhbIjILHGUjZIAPtwXCTa
 YvlPqNtmDMoSaCRvYePlRKhUJBO
 cRodRplUJmbnYOeJPDuAJdbfKmcbQ
 SQLSRdARdthOHuNOTAEZLAMnQTB
 IHZdqDLivShuhxEvckBprZFtmHZWQ
 PMBdFYydCrSFxpioxefBQdUTaWVOB
 bpEVXTVyoMsBgkWnbYywIwhqr
 sXjQybaGGPnPvuvWuODASLsUgLKVq

 if you look closely, you can see the second password is obviously too
long, and
 the last 28 characters are actually the previous password

 if i tell it to print out the length, it never says 64 or whatever, it
still
 pretends the password is just 32.

 i can attatch my code if you want, but its about 600 lines long.

 basically i get a random character, and keep appending random characters
to it
 until its the length it is supposed to be.  i then printf the password,
and if
 its 32 chars long, it will either print out the previous password as a

to the
 end of the password.

 im quite stumped.

 if i only tell it to print out a single generated password, it does this:

 C:\D\myPrograms\passGen>passgen -iterate 1 -min 32 -max 32 -notsayable
 sgJJeNKrfycrhSdYBKPUvEEubCKFswNMThis    is       a fofof        This is a
silly
 silly list


 i can assure you i dont even have the word "silly" anywhere in my code :)

 thanks for listening, i definitely dont pretend to be a great programmer,
so
 feel free to slap me if there's some programming standard i dont know
about that
 does this.

 Nick.
Feb 06 2003
parent "Mike Wynn" <mike.wynn l8night.co.uk> writes:
printf uses "%s" to show a null ( "\0" ) terminated string,
in C char[] foo = " this"
in D char[] foo = "this\0";
printf( "%s", &(foo[0]) ); or cast(char*)

because a D array is a length AND pointer to start of array (C array is just
a pointer to start)

the "%.*s" is used because this tells printf that you are passing a length
and a pointer (not just a pointer).

D arrays are this way so the slice foo[2..n] opperation just creates a new
"array" that has a start pointer &(foo[2]) and length n-2
making it very fast. (at the expence that printf need to be told) and char[]
are NOT quite C char*'s
hence the toStringz( str ) in string.d (phobos).

yes, using cast(char*) returns a pointer to the first element of the array,
but it may be a slice into a much bigger array and not null "\0" terminated
(so printf gets confused).

Mike.

<nick.fletcher dingoblue.com.au> wrote in message
news:b1v2ie$2ehj$1 digitaldaemon.com...
 that worked, thanks!

 could i just ask, whats the difference between

 printf("%s", (char *)str[]);   //   <-- this is what i used

 and

 printf("%.*s", str);    //   <-- this is what worked

 ?
 from what you say, using the first one ignore the length property of the
char
 array and prints till it can print no more?



 In article <b1v1bi$2due$1 digitaldaemon.com>, Mike Wynn says...
if you posted some code it might help, perhaps you can produce a 10 lines
prog that exibits the same behaviour

this is just a guess
you use printf( "%s", cast(char *)str )  ?
try
printf( "%.*s", str )

D strings (char[]) are slices into arrays i.e. struct{pointer to first
char,
length} not null termed strings like C

Mike.

<nick.fletcher dingoblue.com.au> wrote in message
news:b1v0ke$2dit$1 digitaldaemon.com...
 hi, first time posting here, so go easy if ive done something wrong :)

 ive written a password generator in D.  nice language :)
 the problem occurs whenever i output (printf) a passwords that are a
multiple of
 32 characters long.

 it will print out the password, then concatenate the previous password
onto it
 as well.  eg:

 C:\D\myPrograms\passGen>passgen -iterate 10 -min 25 -max 32 -notsayable
 VmJmQMCGalHJmLGkyZCqiKTBneOe
 oXOYHujRqiWIiPjawrQmNBFiCxVPdhhKVmJmQMCGalHJmLGkyZCqiKTBneOe
 MCeGuhbIjILHGUjZIAPtwXCTa
 YvlPqNtmDMoSaCRvYePlRKhUJBO
 cRodRplUJmbnYOeJPDuAJdbfKmcbQ
 SQLSRdARdthOHuNOTAEZLAMnQTB
 IHZdqDLivShuhxEvckBprZFtmHZWQ
 PMBdFYydCrSFxpioxefBQdUTaWVOB
 bpEVXTVyoMsBgkWnbYywIwhqr
 sXjQybaGGPnPvuvWuODASLsUgLKVq

 if you look closely, you can see the second password is obviously too
long, and
 the last 28 characters are actually the previous password

 if i tell it to print out the length, it never says 64 or whatever, it
still
 pretends the password is just 32.

 i can attatch my code if you want, but its about 600 lines long.

 basically i get a random character, and keep appending random
characters
to it
 until its the length it is supposed to be.  i then printf the password,
and if
 its 32 chars long, it will either print out the previous password as a

face)
to the
 end of the password.

 im quite stumped.

 if i only tell it to print out a single generated password, it does
this:
 C:\D\myPrograms\passGen>passgen -iterate 1 -min 32 -max 32 -notsayable
 sgJJeNKrfycrhSdYBKPUvEEubCKFswNMThis    is       a fofof        This is
a
silly
 silly list


 i can assure you i dont even have the word "silly" anywhere in my code
:)
 thanks for listening, i definitely dont pretend to be a great
programmer,
so
 feel free to slap me if there's some programming standard i dont know
about that
 does this.

 Nick.
Feb 06 2003