AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Indexing a string while copying by reference (https://forums.alliedmods.net/showthread.php?t=335945)

Natsheh 01-15-2022 18:20

Indexing a string while copying by reference
 
Will this be a reason of causing an index out of bounds error?

Quote:

new szName[32];
get_user_name(id, szName[10], charsmax(szName));


Shouldn't be this the proper way of using it safely?
:arrow:
PHP Code:

new szName[32];
get_user_name(idszName[10], charsmax(szName)-10); 


Bugsy 01-16-2022 01:48

Re: Indexing a string inside a native will cause outbounds?
 
It makes sense to me. Are you possibly defining the same variable, that is sized differently, globally and locally, accidentally?

Natsheh 01-16-2022 03:28

Re: Indexing a string inside a native will cause outbounds?
 
Nah its just a question out of curiosity.

Can you explain how does it make sense?


For example lets say this is the body function of

PHP Code:

get_user_name(idname[], len)
{
   for(new 
i; <= leni++)
   {
      
name[i] = g_PlayerName[i];
      
// Since we passed 10 to the array cell in the second parameter we advanced 10x4bytes, now we're accessing memory not allocated to us, which will probably cause a crash.

   
}


Yeah so it seems it might cause index out of bounds but also memory access error.

HamletEagle 01-16-2022 03:43

Re: Indexing a string inside a native will cause outbounds?
 
The first one can produce an index out of bounds error since you pass the entire array size as 3rd param but the string is smaller because it is sliced.

The second one looks fine.

Are you actually getting an error? What are you really asking?

Natsheh 01-16-2022 03:49

Re: Indexing a string inside a native will cause outbounds?
 
Quote:

Originally Posted by HamletEagle (Post 2768548)
The first one can produce an index out of bounds error since you pass the entire array size as 3rd param but the string is smaller because it is sliced.

The second one looks fine.

Are you actually getting an error? What are you really asking?


Yeah i am still getting undefined crashes so its best to avoid the first example.

HamletEagle 01-16-2022 06:19

Re: Indexing a string inside a native will cause outbounds?
 
Quote:

Originally Posted by Natsheh (Post 2768549)
Yeah i am still getting undefined crashes so its best to avoid the first example.

It makes perfect sense that you are getting errors using the first code snippet.

PHP Code:

new szName[32];
get_user_name(idszName[10], charsmax(szName)); 

szName has 32 cells but in get_user_name you pass szName[10] which will pass cells 10, 11, 12, ...., 31(a buffer with only 22 cells). Your 3rd argument, the buffer size, is charsmax(szName) which will return 31. 31 > 22 so the native will attempt to write up to 31 characters in a buffer that can only hold 22.

This is the same as doing
PHP Code:

new buf[22]
for(
int i 032i++)
{
    
buf[i] = i



Natsheh 01-16-2022 07:26

Re: Indexing a string inside a native will cause outbounds?
 
Alright then problem solved crisis avoided!!!

Bugsy 01-16-2022 17:24

Re: Indexing a string inside a native will cause outbounds?
 
To clarify, are you still getting the error with the second code with the subtraction? I don't see how you'd get an error with this (second code).

Natsheh 01-21-2022 00:50

Re: Indexing a string inside a native will cause outbounds?
 
Quote:

Originally Posted by Bugsy (Post 2768599)
To clarify, are you still getting the error with the second code with the subtraction? I don't see how you'd get an error with this (second code).

Nope not at all, error is gone.


All times are GMT -4. The time now is 11:30.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.