Mirage Source http://miragesource.net/forums/ |
|
Stop Non-numeric strings from crashing the server. http://miragesource.net/forums/viewtopic.php?f=184&t=5400 |
Page 1 of 38 |
Author: | Labmonkey [ Wed Apr 08, 2009 12:24 am ] |
Post subject: | Stop Non-numeric strings from crashing the server. |
Now I know this way may not be the most secure/effecient way to do this, but it will turn what would be a multiple hour project to be done in a matter of seconds. If you want to do it all manaully you can, but here is the quick easy way. Open up a search box, go to modHandleData. Search for CInt, replace all (in module) with CCInt Search for CLng, replace all (in module) with CCLng Search for CByte, replace all (in module) with CCByte At the bottom add Code: Private Function CCInt(ByRef s As String) As Integer ' Make sure there is a valid string If LenB(s) = 0 Then Exit Function ' Check if it's a number If Not s Like "*[!0-9]*" Then ' Check for overflows If s >= -32768 Then If s <= 32767 Then CCInt = CInt(s) End If End If End If End Function Private Function CCLng(ByRef s As String) As Long ' Make sure there is a valid string If LenB(s) = 0 Then Exit Function ' Check if it's a number If Not s Like "*[!0-9]*" Then ' Check for overflows If s >= -2147483468 Then If s <= 2147483468 Then CCLng = CLng(s) End If End If End If End Function Private Function CCByte(ByRef s As String) As Byte ' Make sure there is a valid string If LenB(s) = 0 Then Exit Function ' Check if it's a number If Not s Like "*[!0-9]*" Then ' Check for overflows If s >= 0 Then If s <= 255 Then CCByte = CByte(s) End If End If End If End Function If you can find any problems please report them. Again I KNOW that this is not the best way to be doing this, but it is such a minuscule difference and I don't know about any of you but I don't want to spend a few hours adding these tedious checks. I would recommend someone doing just that for ms4 though . |
Author: | GIAKEN [ Wed Apr 08, 2009 12:35 am ] |
Post subject: | Re: Stop Non-numeric strings from crashing the server. |
You need to do it like this: Code: Public Function CCInt(ByVal str As String) As Integer
If IsNumeric(str) Then CCInt = CInt(str) Else CCInt = Val(str) End If End Function Public Function CCLng(ByVal str As String) As Integer If IsNumeric(str) Then CCLng = CLng(str) Else CCLng = Val(str) End If End Function Public Function CCByte(ByVal str As String) As Integer If IsNumeric(str) Then CCByte = CCyte(str) Else CCByte = Val(str) End If End Function |
Author: | Labmonkey [ Wed Apr 08, 2009 3:43 pm ] |
Post subject: | Re: Stop Non-numeric strings from crashing the server. |
What does Val do that CInt/CByte/CLng don't? Sorry but it seems like if there was any text in it then there would be errors and it wouldn't matter what you changed the number to. |
Author: | GIAKEN [ Wed Apr 08, 2009 5:36 pm ] |
Post subject: | Re: Stop Non-numeric strings from crashing the server. |
Oh...well I was getting an error with CInt when it was trying to convert "" to 0, so I put a Val on it and it worked. |
Author: | Labmonkey [ Wed Apr 08, 2009 5:40 pm ] |
Post subject: | Re: Stop Non-numeric strings from crashing the server. |
CInt converts a Numeric string ie "0" , "1", "2" to an integer. "" isn't a number. CCInt will convert that to 0 though. Read the code. |
Author: | Jacob [ Wed Apr 08, 2009 5:40 pm ] |
Post subject: | Re: Stop Non-numeric strings from crashing the server. |
I would make the function more like this Code: Public Function CCInt(ByRef s As String) As Integer If Len(s) = 0 Then Exit Function If Not s Like "*[!0-9]*" Then CCInt = CInt(s) End Function * Edit I changed it a little bit. I was testing a little bit more.. If you have a string like "2add2", and use Val(string) it will = 2. With the above method it will equal 0. |
Author: | Joost [ Thu Apr 09, 2009 8:47 am ] |
Post subject: | Re: Stop Non-numeric strings from crashing the server. |
Usning IsNumeric is probably way faster than your code. But yeah, when I told labmonkey about the IsNumeric function, I suggested to check each packet one by one. May be a lot of work, but looks better. |
Author: | Jacob [ Thu Apr 09, 2009 10:52 am ] |
Post subject: | Re: Stop Non-numeric strings from crashing the server. |
I did a speed test and mine was a tiny bit faster. I'll post the results in a little bit. |
Author: | Tony [ Thu Apr 09, 2009 11:56 am ] |
Post subject: | Re: Stop Non-numeric strings from crashing the server. |
Dugor wrote: I did a speed test and mine was a tiny bit faster. I'll post the results in a little bit. How'd you do that? |
Author: | Jacob [ Thu Apr 09, 2009 12:22 pm ] |
Post subject: | Re: Stop Non-numeric strings from crashing the server. |
Testing IsNumeric vs my method. 10000 loops. Quote: %Faster -40| -25| -25| -25| -25| 33.3| -25| -25| 0| -25 Test1 3| 3| 3| 3| 3| 4| 3| 3| 4| 3 Test2 5| 4| 4| 4| 4| 3| 4| 4| 4| 4 There's only a 1 millisecond difference, but that's still something. Test1: Code: Public Sub TestOne() CCInt "test1" End Sub Private Function CCInt(ByRef s As String) As Integer If LenB(s) = 0 Then Exit Function If Not s Like "*[!0-9]*" Then CCInt = CInt(s) End Function Test2: Code: Public Sub TestTwo() CCInt "test2" End Sub Private Function CCInt(ByRef s As String) As Integer If LenB(s) = 0 Then Exit Function If IsNumeric(s) Then CCInt = CInt(s) End Function I use Spodis' speed template for testing: http://www.vbgore.com/Testing_VB_code_speed |
Author: | Dragoons Master [ Thu Apr 09, 2009 6:14 pm ] |
Post subject: | Re: Stop Non-numeric strings from crashing the server. |
Convert to binary packets and you'll have no problem with that at all... |
Author: | Labmonkey [ Thu Apr 09, 2009 6:24 pm ] |
Post subject: | Re: Stop Non-numeric strings from crashing the server. |
Convert to binary packets and you will have to make some free time in your schedule . Anyway, in a bit i will edit my tutorial to use Dugor's method. Thank's for posting. |
Author: | Dragoons Master [ Thu Apr 09, 2009 6:37 pm ] |
Post subject: | Re: Stop Non-numeric strings from crashing the server. |
Labmonkey wrote: Convert to binary packets and you will have to make some free time in your schedule . Anyway, in a bit i will edit my tutorial to use Dugor's method. Thank's for posting. Well, it consumes time, I can't deny, but it's worth the time. |
Author: | Matt [ Thu Apr 09, 2009 6:52 pm ] |
Post subject: | Re: Stop Non-numeric strings from crashing the server. |
Dragoons Master wrote: Labmonkey wrote: Convert to binary packets and you will have to make some free time in your schedule . Anyway, in a bit i will edit my tutorial to use Dugor's method. Thank's for posting. Well, it consumes time, I can't deny, but it's worth the time. I dunno. When Dugor helped me convert FPO to byte array packets, it didn't take that long. |
Author: | Joost [ Fri Apr 10, 2009 8:45 am ] |
Post subject: | Re: Stop Non-numeric strings from crashing the server. |
Dugor, 1 problem with your test, you only test strings. Trow(<someone teach me how to spell this word please) a number in there, and check if there's a difference, considering in 99% the IsNumeric() function will return true, unless some kid is packet editing. Basicly, you tested the wrong thing <3. But I forgive you. This time. Don't do it again. Or I'll kill you. Scotty doesn't know. Dont tell Scotty. |
Author: | Robin [ Fri Apr 10, 2009 8:46 am ] |
Post subject: | Re: Stop Non-numeric strings from crashing the server. |
Joost wrote: Dugor, 1 problem with your test, you only test strings. Trow(<someone teach me how to spell this word please) a number in there, and check if there's a difference, considering in 99% the IsNumeric() function will return true, unless some kid is packet editing. Basicly, you tested the wrong thing <3. But I forgive you. This time. Don't do it again. Or I'll kill you. Scotty doesn't know. Dont tell Scotty. Throw. |
Author: | Jacob [ Fri Apr 10, 2009 12:05 pm ] |
Post subject: | Re: Stop Non-numeric strings from crashing the server. |
I figured i didn't need to post the results for just a regular number because the results were about the same. Quote: %Faster -12.5| -16.7| -28.6| -14.3| 20| -14.3| -14.3| 0| 0| -14.3 Test1 7| 5| 5| 6| 6| 6| 6| 6| 6| 6 Test2 8| 6| 7| 7| 5| 7| 7| 6| 6| 7 Code: Public Sub TestOne() CCInt "222" End Sub Private Function CCInt(ByRef s As String) As Integer If LenB(s) = 0 Then Exit Function If Not s Like "*[!0-9]*" Then CCInt = CInt(s) End Function Code: Public Sub TestTwo()
CCInt "222" End Sub Private Function CCInt(ByRef s As String) As Integer If LenB(s) = 0 Then Exit Function If IsNumeric(s) Then CCInt = CInt(s) End Function |
Author: | Joost [ Fri Apr 10, 2009 12:55 pm ] |
Post subject: | Re: Stop Non-numeric strings from crashing the server. |
In that case, I give up, the function you use is better than the function I suggested. Proving once again discussion increases general knowledge ;p |
Author: | Labmonkey [ Fri Apr 10, 2009 1:14 pm ] |
Post subject: | Re: Stop Non-numeric strings from crashing the server. |
Ok I updated the tutorial. Thanks! |
Author: | Jacob [ Fri Apr 10, 2009 1:17 pm ] |
Post subject: | Re: Stop Non-numeric strings from crashing the server. |
Make sure your functions return the right data type! Code: Private Function CCByte(ByRef s As String) As Integer Code: Private Function CCByte(ByRef s As String) As Byte Code: Private Function CCLng(ByRef s As String) As Integer Code: Private Function CCLng(ByRef s As String) As Long
|
Author: | Jacob [ Fri Apr 10, 2009 1:49 pm ] |
Post subject: | Re: Stop Non-numeric strings from crashing the server. |
I was just thinking, if you want to be truely protected with these conversions, you'll also need to check for overflows. Code: Private Function CCByte(ByRef s As String) As Byte ' Make sure there is a valid string If LenB(s) = 0 Then Exit Function ' Check if it's a number If Not s Like "*[!0-9]*" Then ' Check for overflows If s >= 0 Then If s <= 255 Then CCByte = CByte(s) End If End If End If End Function Code: Private Function CCInt(ByRef s As String) As Integer ' Make sure there is a valid string If LenB(s) = 0 Then Exit Function ' Check if it's a number If Not s Like "*[!0-9]*" Then ' Check for overflows If s >= -32768 Then If s <= 32767 Then CCInt = CInt(s) End If End If End If End Function Code: Private Function CCLng(ByRef s As String) As Long
' Make sure there is a valid string If LenB(s) = 0 Then Exit Function ' Check if it's a number If Not s Like "*[!0-9]*" Then ' Check for overflows If s >= -2147483468 Then If s <= 2147483468 Then CCLng = CLng(s) End If End If End If End Function |
Author: | Labmonkey [ Fri Apr 10, 2009 2:43 pm ] |
Post subject: | Re: Stop Non-numeric strings from crashing the server. |
Thanks Dugor! |
Author: | wanai [ Tue Jan 11, 2022 3:27 pm ] |
Post subject: | Re: Stop Non-numeric strings from crashing the server. |
Econ |
Author: | wanai [ Tue Jan 11, 2022 3:28 pm ] |
Post subject: | Re: Stop Non-numeric strings from crashing the server. |
130.5 |
Page 1 of 38 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |