arrfile$ macro causing my program to crash with files with odd numbers of lines

Started by bhy56, August 02, 2011, 06:06:32 PM

Previous topic - Next topic

ToutEnMasm


You have got the good sample who broke.
I have it with the sample of hutch and the textfile testbrok.txt
Quote
FAULTING_SOURCE_CODE: 
    10:   
    11: ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    12:
    13:     call main
>   14:     inkey
    15:     exit
    16:
    17: ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    18:

Here the result given by windg.I m not sure of it


hutch--

I can duplicate the crash but it does not occur while the code is running, only on exit. This is on the file with the brok uneven line count.


  REPEAT 1024
    call main        ; this works fine
  ENDM

    invoke ExitProcess,0  ; crashes here. comment out and it stops at "inkey"

    inkey
    exit    ; then crashes here which is just ExitProcess.


Changed this line in "arrfile" and it no longer crashes on exit.


; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

arrfilex proc file_name:DWORD

    LOCAL arr   :DWORD
    LOCAL hMem  :DWORD
    LOCAL flen  :DWORD
    LOCAL lcnt  :DWORD
    LOCAL pbuf  :DWORD
    LOCAL spos  :DWORD
    LOCAL void  :DWORD

    push ebx
    mov hMem, InputFile(file_name)
    mov flen, ecx
    add flen, 2 ; <<<<<<<<<<<<<<<<<<<<<<<<<<<< added this
    mov lcnt, rv(get_line_count,hMem,flen)
    mov arr, arralloc$(lcnt)
    mov pbuf, alloc(flen)

    mov spos, 0
    mov ebx, 1
  @@:
    mov spos, rv(readline,hMem,pbuf,spos)
    mov void, arrset$(arr,ebx,pbuf)
    add ebx, 1
    cmp ebx, lcnt
    jle @B

    free pbuf
    free hMem

    mov eax, arr
    pop ebx

    ret

arrfilex endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

bhy56

Quote from: hutch-- on August 05, 2011, 01:39:31 AM

Changed this line in "arrfile" and it no longer crashes on exit.


I added the line, but could you tell me how to re-assemble this so that it is using the new macro?

Thanks

ToutEnMasm

Quote
I added the line, but could you tell me how to re-assemble this so that it is using the new macro?
The arrfile.asm is part of the masm32 lib,(\masm32\m32lib) and you need to rebuild the library.

dedndave

or - just copy the code into your source file, name it something unique, and modify it
that isn't exactly in the spirit of the license - lol
but it works to test the code

it isn't that dificult to modify, assemble, and replace a single module in the masm32 LIB file
it just takes a little experience with LIB, which is handled by the LINKer, i guess
if you go to the command prompt and type
LIB /?
it will show you the switches
MSDN also has a page on LIB - google it

hutch--

bhy56,

Just try the last algo I posted directly in your source code, you will have to manually invoke the procedure and MOV eax to the same return value but this is the easiest way to test it and see if it does the job. I might also say thanks for finding such an obscure bug, when I wrote this system about 4 years ago I tested the hell out of it but never found the bug you reported.

Let us know how it turns out.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

hutch--

 :bg

Don't bve afraid to let us know if the solution worked OK for you. Feedback is important when it comes to fixing obscure bugs in code.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

bhy56

Quote from: hutch-- on August 06, 2011, 02:17:24 AM
:bg

Don't bve afraid to let us know if the solution worked OK for you. Feedback is important when it comes to fixing obscure bugs in code.

I will test it out a little more and see if works for me and then let you know, but it will probably be next week. Thanks for your help so far.

hutch--

Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

dedndave

i gave you a shitload of feedback
but you ignored it
   mov hMem, InputFile(file_name)
   mov lcnt, rv(get_line_count,eax,ecx)
   mov flen, ecx
   mov pbuf, alloc(ecx)
   mov arr, arralloc$(lcnt)

hutch--

Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

dedndave

ok - i have a solution that seems to work
it is odd (PUN) that it has to be this way   :P

the arralloc function starts off like this
    mov eax, mcnt                               ; load the member count into EAX
    add eax, 1                                  ; correct for 1 based array
    lea eax, [eax*4]                            ; multiply it by 4 for memory size

that code makes perfect sense
element 0 is used for the count value
elements 1 through n are for the bstr's

for some strange reason, the spos function wants an even number of elements to work with (not including element 0)

these 3 things makes it work for me....
1) adjust the flen value
2) allocate the pbuf before the arr buff
this code fixes both of those issues
   mov hMem, InputFile(file_name)
   mov lcnt, rv(get_line_count,eax,ecx)
   mov flen, ecx
   mov pbuf, alloc(ecx)
   mov arr, arralloc$(lcnt)

3) make the arralloc function always return an even number of elements (not including element 0)
    mov eax, mcnt                               ; load the member count into EAX
    add eax, 1                                  ; correct for 1 based array
    or  eax, 1                                  ; usable element count always even
    lea eax, [eax*4]                            ; multiply it by 4 for memory size

dedndave


hutch--

Dave,

As best as I can tell at the moment, the problem was a memory overwrite out of range and it appears to be within the granularity of how OLE memory is allocated, my guess is the even numbered counts were fitting into the extra couple of bytes from 4 byte granularity while the odd counts were going out of bounds. OLE memory is no joy to track but its my guess that its a GP fault problem, the error i was getting on exit was an attempt to address 00000000h which is not a valid protected mode address range and it may be because one less than the required count is being allocated and it faults on the last one.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

dedndave

i kind of gave up trying to isolate it any further   :bg

the OLE functions are known to crash programs unexpectedly if one little hair is out of place
that describes the symptom rather well   :P
the only other times i have seen programs crash silently (no dr watson) is when there is a stack imbalance

it may have something to do with the way "null" bstr's are allocated and deallocated