Author Topic: string length bug?  (Read 2451 times)

0 Members and 1 Guest are viewing this topic.

ABer

  • Administrator
  • Hero Member
  • *****
  • Posts: 1260
    • View Profile
string length bug?
« on: August 15, 2020, 07:58:04 AM »
Hi Jean - I found an astrobasic bug (maybe) in the horary script. If the string length exceeds the length set at the variable declaration and the test condition is met, the string doesn't get written to the canvas, but a blank line gets thrown.

In the case below, the if test was met, but the string length was > 66 which I had set with s[66]. The putstring wasn't actioned, but the countLine was incremented. The effect was no string output, but the line was shown blank.

line 2067

 if ((planet_hour = MERCURY or planet_hour = SATURN) && x1 = 1){
     strset(s, "%s", "Planetary hour ruler and ascendant both cold and dry - melancholic");
     putstring(10, 10 + countLine * 30, s);
     countLine += 1;}

I fixed it with s[99] but there was no warning like "string length exceeded" so it took a while to track down the error. It might be helpful to include this warning for coders.

Ed


Jean

  • Administrator
  • Hero Member
  • *****
  • Posts: 1316
    • View Profile
Re: string length bug?
« Reply #1 on: August 16, 2020, 07:48:35 AM »
Hi Ed,
strset() only throws errors if the number of arguments given/provided don't match, if the target string is too small, it just quits, no errors.
In this case, because there are no arguments, strcpy will do fine:

Code: [Select]
strcpy(s, "Planetary hour ruler and ascendant both cold and dry - melancholic");
However strcpy() also has no error checking at all and if you assign a too wide string it will happily overwrite other vars. This is done for speed, it's more or less assumed that the programmer knows she can't put 10 chars in a 5 char array. So i could add the check to strset() but then i would have to do it for other functions like strcpy() too, and i don't like to do that, more code, less speed. In this way, ab is a dangerous language, just like c / c++, it will overwrite memory if you are not careful.
Greetings from Groningen Netherlands.

ABer

  • Administrator
  • Hero Member
  • *****
  • Posts: 1260
    • View Profile
Re: string length bug?
« Reply #2 on: August 16, 2020, 08:41:07 AM »
Hi Jean - thanks for the response - that makes sense. I'll make sure I increase string length to accommodate longer phrases. Ed