Raised This Month: $12 Target: $400
 3% 

Solved Indexing a string while copying by reference


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Natsheh
Veteran Member
Join Date: Sep 2012
Old 01-15-2022 , 18:20   Indexing a string while copying by reference
Reply With Quote #1

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?

PHP Code:
new szName[32];
get_user_name(idszName[10], charsmax(szName)-10); 
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 01-21-2022 at 00:45.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-16-2022 , 01:48   Re: Indexing a string inside a native will cause outbounds?
Reply With Quote #2

It makes sense to me. Are you possibly defining the same variable, that is sized differently, globally and locally, accidentally?
__________________

Last edited by Bugsy; 01-16-2022 at 01:49.
Bugsy is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 01-16-2022 , 03:28   Re: Indexing a string inside a native will cause outbounds?
Reply With Quote #3

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.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 01-16-2022 at 03:44.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 01-16-2022 , 03:43   Re: Indexing a string inside a native will cause outbounds?
Reply With Quote #4

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?
__________________
HamletEagle is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 01-16-2022 , 03:49   Re: Indexing a string inside a native will cause outbounds?
Reply With Quote #5

Quote:
Originally Posted by HamletEagle View Post
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.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 01-16-2022 at 03:51.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 01-16-2022 , 06:19   Re: Indexing a string inside a native will cause outbounds?
Reply With Quote #6

Quote:
Originally Posted by Natsheh View Post
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

__________________

Last edited by HamletEagle; 01-16-2022 at 06:20.
HamletEagle is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 01-16-2022 , 07:26   Re: Indexing a string inside a native will cause outbounds?
Reply With Quote #7

Alright then problem solved crisis avoided!!!
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-16-2022 , 17:24   Re: Indexing a string inside a native will cause outbounds?
Reply With Quote #8

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).
__________________

Last edited by Bugsy; 01-16-2022 at 21:06.
Bugsy is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 01-21-2022 , 00:50   Re: Indexing a string inside a native will cause outbounds?
Reply With Quote #9

Quote:
Originally Posted by Bugsy View Post
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.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 13:54.


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