It is currently Fri Jul 19, 2019 4:48 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Pointer bug
PostPosted: Sun Feb 10, 2019 1:57 pm 
Offline
Addict
Addict

Joined: Wed Nov 12, 2008 5:01 pm
Posts: 1046
Location: Russia
Code:
For i = 0 To 9
  ;Debug (@"1234567890")+i*2
  Debug PeekS((@"1234567890")+i*2, 1)
Next
Result should be.
Code:
1
2
3
4
5
6
7
8
9
0

_________________
Library XP_Menu_Lib - office menu; HID_Lib - USB Library
Torrent client - pbTorrent (source code); Create driver in PureBasic.


Top
 Profile  
Reply with quote  
 Post subject: Re: Pointer bug
PostPosted: Sun Feb 10, 2019 2:16 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 19, 2011 3:47 am
Posts: 2213
Location: Singapore
User_Russian wrote:
Code:
For i = 0 To 9
  ;Debug (@"1234567890")+i*2
  Debug PeekS((@"1234567890")+i*2, 1)
Next
Result should be.
Code:
1
2
3
4
5
6
7
8
9
0

It won't work with string literals.
Code:
x$ = "1234567890"
For i = 0 To 9 
  Debug PeekS((@x$) + i*2, 1)
Next

_________________
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too!


Top
 Profile  
Reply with quote  
 Post subject: Re: Pointer bug
PostPosted: Sun Feb 10, 2019 2:26 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Jul 19, 2013 7:36 am
Posts: 183
Location: Novosibirsk
Surprisingly, in this case, the pointer moves

Code:
For i = 0 To 9
  Debug PeekS(@"1234567890"+i*2) ;without specifying the length.
Next


1234567890
234567890
34567890
4567890
567890
67890
7890
890
90
0

win10x64 PB5.70x64

p.s.
Code:
For i = 0 To 9 
  Debug PeekS(@"1234567890" + i*2, 1, #PB_Unicode)
Next

and there's a memory access error

_________________
Dawn will come inevitably.
His (http://translate.yandex.ru/) English is not as good as we would like
366_pairs_of_wings, Angel, Guardian, for_a_good_mood, for_every_days


Top
 Profile  
Reply with quote  
 Post subject: Re: Pointer bug
PostPosted: Sun Feb 10, 2019 10:36 pm 
Offline
Addict
Addict
User avatar

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 3559
Location: Utah, USA
Use:
Code:
*d = @"1234567890"
For i = 0 To 9
  Debug PeekS(*d + i * 2, 1)
Next

_________________
Image


Top
 Profile  
Reply with quote  
 Post subject: Re: Pointer bug
PostPosted: Mon Feb 11, 2019 2:29 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Jul 19, 2013 7:36 am
Posts: 183
Location: Novosibirsk
Demivec wrote:
Use:
Code:
*d = @"1234567890"
For i = 0 To 9
  Debug PeekS(*d + i * 2, 1)
Next

It's obvious.
-------------------------------------
But the point is that a formally correct construction which does not cause compiler errors generates a non-working code and it is also obvious.

_________________
Dawn will come inevitably.
His (http://translate.yandex.ru/) English is not as good as we would like
366_pairs_of_wings, Angel, Guardian, for_a_good_mood, for_every_days


Top
 Profile  
Reply with quote  
 Post subject: Re: Pointer bug
PostPosted: Mon Feb 11, 2019 3:04 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Aug 31, 2005 11:09 pm
Posts: 3694
Location: Italy
Uhmmm... it should work, agree it's a bug.
These two should be equivalent but they aren't:

Code:
For i = 0 To 9
  a$ = PeekS((@"1234567890")+i*2, 1)
  Debug a$ ; outputs nothing on my pc
Next

For i = 0 To 9   
  *p = @"1234567890" 
  a$ = PeekS((*p)+i*2, 1)
  Debug a$ ; outputs 1,2,3 ... 0
Next


In both cases the literal string is stored just once in the data section, so the resulting address in both cases should be constant.
And it is.
But in the first case PB makes a temporary copy of the string on each loop which is not even used, but that code is causing the bug.

The code generated (x86) for the body of the first snippet is:

Code:
For i = 0 To 9
  a$ = PeekS((@"1234567890")+i*2, 1)
  Debug a$ ; outputs nothing
Next

For i = 0 To 9
EnableASM
  MOV    edx,[_PB_StringBasePosition]
  PUSH   edx
  PUSH   edx
  PUSH   dword 1
  MOV    eax,_S1
  MOV    ebx,eax
 
  ; this part is useless and screws up what's following
  PUSH   dword [_PB_StringBasePosition] ; future stack overflow
  MOV    edx,_S1
  PUSH   edx
  CALL  _SYS_CopyString@4
  ; ---------------------------------------------------
 
  MOV    edi,dword [v_i]
  ADD    edi,edi
  ADD    ebx,edi
  PUSH   ebx
  CALL  _PB_PeekS2@12
  PUSH   dword v_a$
  CALL  _SYS_AllocateString4@8
DisableASM
  Debug a$
Next

For i = 0 To 9
EnableASM
  MOV    edx,[_PB_StringBasePosition]
  PUSH   edx
  PUSH   edx 
  MOV    eax,_S1
  MOV    ebx,eax ; points to the literal string
 
  ; removing this makes it work
  ; PUSH   dword [_PB_StringBasePosition]
  ; MOV    edx,_S1
  ; PUSH   edx
  ; CALL  _SYS_CopyString@4
 
  MOV    edi,dword [v_i] ; load the index loop var
  ADD    edi,edi ; multiply it by two
  ADD    ebx,edi ; add the offset to the string base pointer
  PUSH   dword 1 ; 1 unicode char to be read by peeks()
  PUSH   ebx ; the first param of peeks()
 
  CALL  _PB_PeekS2@12 ; now it works
  PUSH   dword v_a$
  CALL  _SYS_AllocateString4@8
DisableASM
  Debug a$
Next


The edited code works, the first one if put in a loop would eventually lead to a stack overflow.

_________________
[ My little PureBasic review ]


Top
 Profile  
Reply with quote  
 Post subject: Re: Pointer bug
PostPosted: Mon Feb 11, 2019 7:36 pm 
Offline
Addict
Addict
User avatar

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 3559
Location: Utah, USA
An error also happens when the length is not specified for PeekS() in this slight variation:
Code:
*d = @"1234567890"
For i = 0 To 9
  b$ = PeekS((@"1234567890") + i * 2) ;memory access error occurs here on first time through loop
  a$ = PeekS(*d + i * 2, 1)
  Debug a$
  Debug b$
Next


Win 8.1 64-bit

_________________
Image


Last edited by Demivec on Tue Feb 12, 2019 1:41 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Pointer bug
PostPosted: Mon Feb 11, 2019 8:29 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 13, 2010 3:45 pm
Posts: 861
Demivec wrote:
An error also happens when the length is not specified for PeekS() in this slight variation

Win7 x32 ok
Win7 x64 error

_________________
sorry for my bad english


Top
 Profile  
Reply with quote  
 Post subject: Re: Pointer bug
PostPosted: Tue Feb 12, 2019 12:21 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Aug 31, 2005 11:09 pm
Posts: 3694
Location: Italy
Josh wrote:
Win7 x32 ok


The problem is there, it may just not be always visible in short code (try to put that code in a loop).

Code:
For j = 0 To 99999
For i = 0 To 9 
  *d = @"1234567890"
  b$ = PeekS((@"1234567890") + i * 2) ;memory access error occurs here AFTER SOME ITERATIONS
  a$ = PeekS(*d + i * 2, 1)
  ;Debug a$
  ;Debug b$
Next
Next

_________________
[ My little PureBasic review ]


Top
 Profile  
Reply with quote  
 Post subject: Re: Pointer bug
PostPosted: Wed Feb 13, 2019 11:44 am 
Offline
Addict
Addict
User avatar

Joined: Wed Aug 31, 2005 11:09 pm
Posts: 3694
Location: Italy
The stack overflow happens even without a function call

Code:
For j = 0 To 99999
For i = 0 To 9
  a = @"1234567890" + i ; stack overflow when @"string" is used in an expression
Next
Next


All the strange things saw in the thread are caused by this stack imbalance.

_________________
[ My little PureBasic review ]


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye