Mirage Engine
Stop Non-numeric strings from crashing the server. - Printable Version

+- Mirage Engine (https://mirage-engine.uk/forums)
+-- Forum: Mirage Source (Nostalgia) (https://mirage-engine.uk/forums/forumdisplay.php?fid=61)
+--- Forum: Archive (2006-2011) (https://mirage-engine.uk/forums/forumdisplay.php?fid=18)
+---- Forum: Source Code Development (https://mirage-engine.uk/forums/forumdisplay.php?fid=51)
+----- Forum: Mirage Source 4 (Visual Basic 6) (https://mirage-engine.uk/forums/forumdisplay.php?fid=44)
+------ Forum: Bugs Reports (https://mirage-engine.uk/forums/forumdisplay.php?fid=9)
+------ Thread: Stop Non-numeric strings from crashing the server. (/showthread.php?tid=2702)



Stop Non-numeric strings from crashing the server. - Labmonkey - 08-04-2009

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 = -2147483468 Then
If s = 0 Then
If s


Re: Stop Non-numeric strings from crashing the server. - GIAKEN - 08-04-2009

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



Re: Stop Non-numeric strings from crashing the server. - Labmonkey - 08-04-2009

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.


Re: Stop Non-numeric strings from crashing the server. - GIAKEN - 08-04-2009

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.


Re: Stop Non-numeric strings from crashing the server. - Labmonkey - 08-04-2009

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.


Re: Stop Non-numeric strings from crashing the server. - Jacob - 08-04-2009

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.


Re: Stop Non-numeric strings from crashing the server. - Joost - 09-04-2009

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.


Re: Stop Non-numeric strings from crashing the server. - Jacob - 09-04-2009

I did a speed test and mine was a tiny bit faster. I'll post the results in a little bit.


Re: Stop Non-numeric strings from crashing the server. - Tony - 09-04-2009

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?


Re: Stop Non-numeric strings from crashing the server. - Jacob - 09-04-2009

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


Re: Stop Non-numeric strings from crashing the server. - Dragoons Master - 09-04-2009

Convert to binary packets and you'll have no problem with that at all...


Re: Stop Non-numeric strings from crashing the server. - Labmonkey - 09-04-2009

Convert to binary packets and you will have to make some free time in your schedule Wink.


Anyway, in a bit i will edit my tutorial to use Dugor's method. Thank's for posting.


Re: Stop Non-numeric strings from crashing the server. - Dragoons Master - 09-04-2009

Labmonkey Wrote:Convert to binary packets and you will have to make some free time in your schedule Wink.


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.


Re: Stop Non-numeric strings from crashing the server. - Matt - 09-04-2009

Dragoons Master Wrote:
Labmonkey Wrote:Convert to binary packets and you will have to make some free time in your schedule Wink.


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.


Re: Stop Non-numeric strings from crashing the server. - Joost - 10-04-2009

Dugor, 1 problem with your test, you only test strings. Trow(


Re: Stop Non-numeric strings from crashing the server. - Robin - 10-04-2009

[quote="Joost"]Dugor, 1 problem with your test, you only test strings. Trow(


Re: Stop Non-numeric strings from crashing the server. - Jacob - 10-04-2009

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



Re: Stop Non-numeric strings from crashing the server. - Joost - 10-04-2009

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


Re: Stop Non-numeric strings from crashing the server. - Labmonkey - 10-04-2009

Ok I updated the tutorial. Thanks!


Re: Stop Non-numeric strings from crashing the server. - Jacob - 10-04-2009

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



Re: Stop Non-numeric strings from crashing the server. - Jacob - 10-04-2009

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 = -32768 Then
If s = -2147483468 Then
If s


Re: Stop Non-numeric strings from crashing the server. - Labmonkey - 10-04-2009

Thanks Dugor!