13 Dec, 2016, Rhien wrote in the 1st comment:
Votes: 0
Curious for a second set of eyes on whether I fixed something well.

I downloaded Davion's grid code, compiled and used his example which crashed on first try. GDB indicated that crashing line was " if ((*last_lr + 1) != '\0')". As I can see in GDB, the cell passed in didn't have a "\n" in the contents so *last_lr never got set therefore accessing it and caused the crash.

To fix, I set the "int count = 1" (assuming that everyone has 1 line if I'm calling this).

My question for anyone who has used this, do you see any issues with my fix? I have not broken it thus far and it seems to work as expected now.

Crashed:

//Low level line counter
void cell_set_linecount(GRID_CELL *cell)
{
int count = 0;
char *pos = cell->contents;
char *last_lr;
while (*pos++)
if (*pos == '\n')
{
last_lr = pos;
count++;
}
if ((*last_lr + 1) != '\0')
count++;

cell->lines = count;

if (cell->row->max_height < count)
cell->row->max_height = count;
}


My fix which appears to work:

//Low level line counter
void cell_set_linecount(GRID_CELL *cell)
{
int count = 1;
char *pos = cell->contents;
char *last_lr;
while (*pos++)
if (*pos == '\n')
{
last_lr = pos;
count++;
}

cell->lines = count;

if (cell->row->max_height < count)
cell->row->max_height = count;
}


Also, love the end result, cool code Davion.
14 Dec, 2016, Davion wrote in the 2nd comment:
Votes: 0
Hey Rhien!
Thanks for the shoutout. I'm not really sure why I never caught that in testing, but I usually have things in places for a reason, even if they don't always work.

I'd suggest replacing it with the following to make sure whatever that was doing, it keeps doing it.

if (last_lr && (*last_lr + 1) != '\0')
count++;
15 Dec, 2016, Rhien wrote in the 3rd comment:
Votes: 0
Thanks for the response, I'll add that and give it a try. I thought I had added a NULL check but maybe I dreamed that (or did it but didn't copyover, I'll put nothing past me when coding late at night, hehe).

I had set the line counter to 1 by default here (assuming everything that calls it will send 1 line even if blank) and removed the last_lr all together (if there are no \n's it would be 1 line, then if i found some in the loop that was already there it would increment it.. 1 found \n would make 2 lines).

That seems to work in everything I've thrown at it. I unleashed 30 python mud bots from my Raspberry Pi onto the mud spamming a command that that called the grid code for a few hours and it appears to be leaking nothing.

The other change I made was to fix a compiler warning saying a strncat call had a potential buffer overrun so I changed this ("warning: call to __builtin___strncat_chk might overflow destination buffer"):

strncat(cell->contents, buf, MSL);


To

strncat(cell->contents, buf, MSL - 1);


Thanks again for sharing your code, it's very cool (and very easy to use). I added a few methods to specify padding on the fly. Using it to make a new score command and it's working great (will have the player able to tailor their score to snap in pieces they want to see so it can be as compact or detailed as they like).
16 Dec, 2016, agda wrote in the 4th comment:
Votes: 0
You still need to initialize the pointer and the if-statment seems wrong.
The logic should be "!last_lr || …" and also "(*last_lr + 1)" does not
point to the next char after "*last_lr" but increament the char value that
"last_lr" points to.

And Hi!

int count = 0;
char *last_lr = null;



/*
* For text such as
* 1) "line nr 1" and
* 2) "line nr 1\nline nr 2"
* add one to count.
* But not:
* 3) "line nr 1\nline nr 2\n"
*/
if (!last_lr || *(last_lr + 1) != '\0')
count++;
**
0.0/4