Why is this working?
#1

Code:
new array[10];
strcat(array, "12345678901", 12); // string is 11 characters
printf("%s\nLen: %i", array, strlen(array));

Prints out:
12345678901
Len: 11
Why is that working? Attempting to do it manually will always show an error:
Code:
array[10] = '1';
Unable to compile that

new i = 10;
array[i] = '1';
The compiler won't detect that, but crashdetect will.
But if you go over 11 characters (in my example), crashdetect will start printing out errors in the console.
Reply
#2

That's the difference between run time and compile time.
Like if you put a number itself 'array[10]', 10 is the size and index should be < 10; compiler gives you an error because 10 is a constant but if you use a variable, it can be anything, compiler cannot predict values, so you'll get run time in that case.

And "strcat" might have internal checks to avoid run time errors.

Edit: i misread strcat part, thats probably overwriting the EOS index of "array". Which i believe is a PAWN bug!
Reply
#3

That might be because next memory cells after array[10] is empty and it finally finds the EOS char 2 memory cells after array, but it still throws a runtime error as far as I remember and this is essentially what memory leaking is, gonna make serious problems later on if you don't fix it. (again I'm not sure how pawn works regarding memories, just a guess seeing as how pawn deals with these things in general.)

Edit: there was also a trick in one of Yashas' topics about this pawn trick/bug with static vars.
Reply
#4

All initialisation occurs on runtime so compiler won't detect them as they can't predict the variables(name itself says) but only constants if you used const keyword then compiler would give error.
Reply
#5

I'm not talking about the compiler not detecting them, I'm talking about why the array actually holds that string, even though its length is bigger than the array's size.
Reply
#6

Quote:
Originally Posted by Stinged
View Post
I'm not talking about the compiler not detecting them, I'm talking about why the array actually holds that string, even though its length is bigger than the array's size.
Well for that I have same answer as gammix said
Did u try to print that string character wise?(a loop till null) Just to verify if that prints something else too or ening up in a infinite loop then its overwriting null terminator.

Edit: Oh yeah its giving infinite loop null is @ the beginning of string (bug confirmed strcat is not setting null terminator to the end after concatenating when size is exceeded) and sometimes even increasing size giving some of the letters (4 and 8 in this case idk why)
Reply
#7

Quote:
Originally Posted by Gammix
View Post
Edit: i misread strcat part, thats probably overwriting the EOS index of "array". Which i believe is a PAWN bug!
** I'm just guessing here **

I don't think this is a bug.
Functions that take arrays(strings) as parameters have no idea how large that array is, so it is scripters' job to properly report array size in the size parameter.
Since OP reported a larger size, strcat overwritten the next 2 cells that were not allocated to the array with '1' and NUL char.

Printf and strlen don't take any size parameter, but they rely on the fact that any string ends at the first NUL char found, which, in OP case, is +2 cells after the array end.
Reply
#8

It's not really a problem since I never actually do that, but it just happened by mistake and it got me wondering.
Anyway, thank you for the replies.
Reply
#9

Quote:
Originally Posted by Stinged
View Post
I'm not talking about the compiler not detecting them, I'm talking about why the array actually holds that string, even though its length is bigger than the array's size.
It doesn't. This is known as buffer overflow. Retrieving a string from memory is basically saying: start reading at this address and continue reading until null (\0) is encountered. Thereby possibly extending into address space occupied by other variables if you're lucky or into address space reserved for critical processes if you're not so lucky. In the latter case the server will probably crash.

Create another array right after your existing one. Don't store anything in it. Do your strcat stuff and then print out both arrays afterwards. Although not guaranteed, it is reasonable to assume that the second array will be placed adjacent to the first one in memory, so even though you didn't explicitly store anything in the second array it will probably contain some text.
Reply
#10

Quote:
Originally Posted by Vince
View Post
It doesn't. This is known as buffer overflow. Retrieving a string from memory is basically saying: start reading at this address and continue reading until null (\0) is encountered. Thereby possibly extending into address space occupied by other variables if you're lucky or into address space reserved for critical processes if you're not so lucky. In the latter case the server will probably crash.

Create another array right after your existing one. Don't store anything in it. Do your strcat stuff and then print out both arrays afterwards. Although not guaranteed, it is reasonable to assume that the second array will be placed adjacent to the first one in memory, so even though you didn't explicitly store anything in the second array it will probably contain some text.
Oh I see, thanks!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)