String manipulation bug

Just starting out? Need help? Post your questions and find answers here.
User avatar
RichAlgeni
Addict
Addict
Posts: 914
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

String manipulation bug

Post by RichAlgeni »

Found this bug while migrating to Unicode, the premise of which is an error Poking a string, after creating a large string.

Here is the code, then I will explain:

Code: Select all

; test_ascii_strings.pb

EnableExplicit

OpenConsole()

PrintN("#PB_Compiler_Version = " + #PB_Compiler_Version)
PrintN("#PB_Compiler_Unicode = " + #PB_Compiler_Unicode)
PrintN("")
Define dateStringMask.s  = "yyyy-mm-dd"
Define dateStringLength.i = StringByteLength(dateStringMask, #PB_Ascii)

Define *baseWebFile
Define baseWebFile.s
Define tempIt.s = "$$$$$$$$$$$$$$ 15:26:44 GetDataFromRedis()#######> Sent 'yyyy-mm-dd' bytes for GET /ambulance/cad/active_calls.xml, yyyy-mm-dd correct $$$$$$$$$$$$$$"

Define i.i
Define j.i = 200
PrintN("Number of loops: " + Str(j))
For i=1 To j
    baseWebFile + tempIt
Next

Define fileLength.i = StringByteLength(baseWebFile, #PB_Ascii)
PrintN("fileLength = " + Str(fileLength))
Define *baseWebFile = AllocateMemory(fileLength)
PokeS(*baseWebFile, baseWebFile, fileLength, #PB_Ascii)
Define *dateStringMaskAscii
PrintN("Press <enter> to continue to next step:")
Input()

*dateStringMaskAscii = AllocateMemory(dateStringLength)
PokeS(*dateStringMaskAscii, dateStringMask, dateStringLength, #PB_Ascii)
PrintN("Program complete. Press <enter> to quit")
Input()

CloseConsole()
End
Modify line 19, 'j'.
When j is set to 199, the program completes successfully.
When j is set to 200, the program will die at line 33, PokeS(*dateStringMaskAscii, dateStringMask, dateStringLength, #PB_Ascii)
When j is set to 201, the program completes successfully.

If line 15, tempIt is changed, j can be set differently before the program dies.

If I remove one character from tempIt on line 15, the program dies when j is set to 202.

This happens in all versions I tested, 5.46, 5.62 and 5.70.

Here are the individual results:

Code: Select all

================================================================================================

D:\Dev\PureBasic\PureBasicTesting>test_ascii_strings
#PB_Compiler_Version = 546
#PB_Compiler_Unicode = 0

Number of loops: 199
fileLength = 29651
Press <enter> to continue to next step:

Program complete. Press <enter> to quit

================================================================================================

D:\Dev\PureBasic\PureBasicTesting>test_ascii_strings
#PB_Compiler_Version = 562
#PB_Compiler_Unicode = 1

Number of loops: 199
fileLength = 29651
Press <enter> to continue to next step:

Program complete. Press <enter> to quit

================================================================================================

D:\Dev\PureBasic\PureBasicTesting>test_ascii_strings
#PB_Compiler_Version = 570
#PB_Compiler_Unicode = 1

Number of loops: 199
fileLength = 29651
Press <enter> to continue to next step:

Program complete. Press <enter> to quit

================================================================================================
================================================================================================

D:\Dev\PureBasic\PureBasicTesting>test_ascii_strings
#PB_Compiler_Version = 546
#PB_Compiler_Unicode = 0

Number of loops: 200
fileLength = 29800
Press <enter> to continue to next step:

================================================================================================

D:\Dev\PureBasic\PureBasicTesting>test_ascii_strings
#PB_Compiler_Version = 562
#PB_Compiler_Unicode = 1

Number of loops: 200
fileLength = 29800
Press <enter> to continue to next step:

================================================================================================

D:\Dev\PureBasic\PureBasicTesting>test_ascii_strings
#PB_Compiler_Version = 570
#PB_Compiler_Unicode = 1

Number of loops: 200
fileLength = 29800
Press <enter> to continue to next step:

================================================================================================
================================================================================================

D:\Dev\PureBasic\PureBasicTesting>test_ascii_strings
#PB_Compiler_Version = 546
#PB_Compiler_Unicode = 0

Number of loops: 201
fileLength = 29949
Press <enter> to continue to next step:

Program complete. Press <enter> to quit

================================================================================================

D:\Dev\PureBasic\PureBasicTesting>test_ascii_strings
#PB_Compiler_Version = 562
#PB_Compiler_Unicode = 1

Number of loops: 201
fileLength = 29949
Press <enter> to continue to next step:

Program complete. Press <enter> to quit

================================================================================================

D:\Dev\PureBasic\PureBasicTesting>test_ascii_strings
#PB_Compiler_Version = 570
#PB_Compiler_Unicode = 1

Number of loops: 201
fileLength = 29949
Press <enter> to continue to next step:

Program complete. Press <enter> to quit

================================================================================================
Where you see 'Program complete. Press <enter> to quit', the program worked correctly. Not sure why, I can't an OnErrorCall to give me any information. Purifier is enabled. I wanted to include just the barebones program, to see if people can recreate the issue.

Also, I don't know how I didn't hit this before, I guess the circumstances needed to be exactly right.

Luckily, this bug only took down an ambulance dispatch testing server.
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Re: String manipulation bug

Post by nco2k »

Code: Select all

AllocateMemory(n + SizeOf(Ascii))
or

Code: Select all

PokeS(*ptr, str$, n, #PB_Ascii | #PB_String_NoZero)
c ya,
nco2k
Last edited by nco2k on Tue May 07, 2019 11:34 pm, edited 1 time in total.
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
User avatar
RichAlgeni
Addict
Addict
Posts: 914
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: String manipulation bug

Post by RichAlgeni »

nco2k wrote:

Code: Select all

AllocateMemory(n + SizeOf(Character))
or

Code: Select all

PokeS(*ptr, str$, n, #PB_Ascii | #PB_String_NoZero)
c ya,
nco2k
No.
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Re: String manipulation bug

Post by nco2k »

yes.

Code: Select all

; test_ascii_strings.pb

EnableExplicit

OpenConsole()

PrintN("#PB_Compiler_Version = " + #PB_Compiler_Version)
PrintN("#PB_Compiler_Unicode = " + #PB_Compiler_Unicode)
PrintN("")
Define dateStringMask.s  = "yyyy-mm-dd"
Define dateStringLength.i = StringByteLength(dateStringMask, #PB_Ascii)

Define *baseWebFile
Define baseWebFile.s
Define tempIt.s = "$$$$$$$$$$$$$$ 15:26:44 GetDataFromRedis()#######> Sent 'yyyy-mm-dd' bytes for GET /ambulance/cad/active_calls.xml, yyyy-mm-dd correct $$$$$$$$$$$$$$"

Define i.i
Define j.i = 200
PrintN("Number of loops: " + Str(j))
For i=1 To j
    baseWebFile + tempIt
Next

Define fileLength.i = StringByteLength(baseWebFile, #PB_Ascii)
PrintN("fileLength = " + Str(fileLength))
Define *baseWebFile = AllocateMemory(fileLength + SizeOf(Ascii))
PokeS(*baseWebFile, baseWebFile, fileLength, #PB_Ascii)
Define *dateStringMaskAscii
PrintN("Press <enter> to continue to next step:")
Input()

*dateStringMaskAscii = AllocateMemory(dateStringLength + SizeOf(Ascii))
PokeS(*dateStringMaskAscii, dateStringMask, dateStringLength, #PB_Ascii)
PrintN("Program complete. Press <enter> to quit")
Input()

CloseConsole()
End
edit: oops i meant n + SizeOf(Ascii). either way, both works with your code.

c ya,
nco2k
Last edited by nco2k on Tue May 07, 2019 11:35 pm, edited 1 time in total.
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
User avatar
RichAlgeni
Addict
Addict
Posts: 914
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: String manipulation bug

Post by RichAlgeni »

I understand what you did, it works, and I appreciate your input.

But why does my version work with 199 or 201 loops, and not 200?

Should it be consistent???
User avatar
RichAlgeni
Addict
Addict
Posts: 914
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: String manipulation bug

Post by RichAlgeni »

Fred, Freak and the powers that be can mark this as 'not a bug' if needed.

Thanks to 'nco2k' for pointing out the issue with not including '#PB_String_NoZero'.

I just have to question the inconsistency. And that it might throw others a curve as well.
Post Reply