Page 1 of 1

atr_add2ent unexpected behaviour (Classic DCAL)

PostPosted: Sun Mar 25, 2018 5:38 pm
by dhs
Hi,
I have found some unexpected behaviour with the atr_add2ent procedure. Although the attribute is added with the correct name. It appears that the name in the attrib variable can be bmodified by atr_add2ent (and presumably also other atr_add procedures).
Below is an extract of my DCAL code that uses atr_add2ent:
Code: Select all   strassign (atr.name, tag);
   LogStrStr ('Tag assigned', atr.name);          ! LogStrStr is my procedure and writes 2 string values to a file
   atr.str := 'dhSpacePlannerTag_';
   AppendDateTimeStamp (atr.str, true, true, false, false, true);
   LogStrStr ('About to add tag', atr.name);      !atr.name has the expected 12 character name at this point
   atr_add2ent (ent, atr);
   LogStrStr ('Tag added', atr.name);             !atr.name appears to have a space appended to it
   LogStrInt ('Tag added', strlen(atr.name));     !strlen(atr.name) returns 13    (LogStrInt  is my procedure and writes a string and an integer string to a file)
   if atr_entfind (ent, tag, atr) then
      LogStrStr ('Tag retrieved', atr.name);      !the attribute has been added to the entity correctly, and is retrieved with the correct 12 character name
   end;

I've added notes about the results in the comments above, but basically atr.name was set to an invalid 13 character value by the atr_add2ent call. The macro was crashing with 'string too long' errors in subsequent logic, and it took me much time to find the cause (I don't normally add quite so many logging calls!).
Posting this information in case it saves somebody else the angst that it caused me. I have aslo added some notes regarding this to my version of the DCAL Manual (http://www.dhsoftware.com.au/DCAL.pdf).

Rgds,
David H

Re: atr_add2ent unexpected behaviour (Classic DCAL)

PostPosted: Wed Mar 28, 2018 10:14 pm
by Jsosnowski
Interesting. I believe attribute names are supposed to be limited to 12 characters ... so what's up here? I'm surprised it is not producing an exception error.
I am still working in D4D in other areas, but expect to get back to some attribute work in a couple weeks. I'll check out this behavior in D4D and add my results then.

Re: atr_add2ent unexpected behaviour (Classic DCAL)

PostPosted: Thu Mar 29, 2018 12:58 am
by dhs
Yes, atrname is a 12 character string. The length of the string is stored in the first byte of the string variable (and this is what strlen is returning), so it is feasible to set it to a number greater than the allowable length without actually causing an error. But any subsequent use of the string is likely to (and did in my case) cause problems.
Will be interesting to see if you find the same behaviour in D4D (at least it should be easy to debug in Delphi ... I spent considerable time tracking down exactly what was causing my problems). I might have written my current project in D4D if the modeGroup bug had been fixed 7 or 8 months earlier, and it would have been a lot easier to debug if I had (but I'm a long way down the track now, and not really inclined to convert to D4D).
Rgds,
David H.

Re: atr_add2ent unexpected behaviour (Classic DCAL)

PostPosted: Thu Mar 29, 2018 9:37 am
by Jsosnowski
I believe that shortStrings (aka str255) hold the length in the string [0] position and that this byte is not counted in calls to strlen (at least according to the DCAl manual = see string discussion on pg 28/29). Delphi
s length method does the same thing. Does your returned atr.name value have an additional space added when it is returned with an extra character in its length?

Re: atr_add2ent unexpected behaviour (Classic DCAL)

PostPosted: Thu Mar 29, 2018 12:25 pm
by dhs
Yes, you are correct about the length in the [0] position ... that is what I meant by the first byte of the variable, although perhaps my wording was a bit ambiguous. The atrname written to my log file has an extra space added to the end (at position 13 or the 12 character string), and strlen returns 13. I think this is more due to the length byte (offset 0) being changed to an invalid value rather than a space being appended to the string as such. The 'adding' of a space or any other particular character may be more to do with the value of the adjacent variable in memory as I believe the variables are packed rather than word aligned (although in all cases where I have logged this it has been a space that has been written to the log file and not any other character).