flat assembler
Message board for the users of flat assembler.

Index > Windows > FASM array overwriting itself

Author
Thread Post new topic Reply to topic
patchariadog



Joined: 24 Mar 2013
Posts: 94
patchariadog
I am trying to get a list of all files in a listbox and all folders in an array so then I can cycle through the array and perform the "get files function" so I can get all files in subfolders aswell. the problem is with the folder array. I researched how to do arrays on fasm and came up with this example that works fine on its own program

Code:

include 'win32ax.inc'
        str0    db 'Array0',0
        str1    db 'Array1',0
        str2    db 'Array2',0
        str3    db 'Array3',0

        array   rd 4
main:
        mov     [array], str0
        mov     [array+4], str1
        mov     [array+8], str2
        mov     [array+12], str3
        invoke  MessageBox, 0, [array], 0, 0
        invoke  MessageBox, 0, [array+4], 0, 0
        invoke  MessageBox, 0, [array+8], 0, 0
        invoke  MessageBox, 0, [array+12], 0, 0
        invoke  ExitProcess, 0
.end main 

    


this makes it where array holds str0, array+4 holds str1 and so on

this is my button code
Code:
 .button4:;get folder
invoke SHBrowseForFolderA, folderdialog
.if eax = 0
ret
.endif
mov [hFile],eax
invoke SHGetPathFromIDListA,eax,buffer1
invoke MessageBoxA, NULL,buffer1, title, NULL
invoke lstrcatA,buffer1,"\"
invoke lstrcpyA,filename,buffer1
invoke lstrcatA,buffer1,"*.*"
invoke MessageBoxA, NULL,buffer1, title, NULL

mov edi,1

sub esp, 0x140                      ; Free some space on the stack for our WIN32_FIND_DATA struct
mov ebx, esp                        ; ebx now holds our struct
push ebx
push buffer1
call [FindFirstFileA]
test eax, eax
jz .list_end2
mov [hFindFile], eax
lea esi, [ebx+0x2C]               ; = FileName
mov ecx, [ebx+0x20]               ; = FileSize
.fileloop2:
push ebx
push [hFindFile]
call [FindNextFileA]
test eax, eax
jz .list_end2                        ; If there're no more files, exit the loop
mov ecx, [ebx+0x20]               ; = FileSize (in Bytes). Is 0 when it's a directory

.if edi = 1
mov edi,0
mov [count],0
jmp .fileloop2
.endif

.if ecx = 0
invoke lstrcpyA,buffer2,filename
invoke lstrcatA,buffer2,esi
mov ecx,[count]
mov eax,4
mul ecx
mov dword [array+eax],buffer2
invoke  MessageBoxA, 0, [array+eax], 0, 0
invoke SendDlgItemMessageA, [hwnd], folderslistbox, LB_ADDSTRING, 0, buffer2
mov ecx,0
inc [count]
.endif

.if ecx <> 0
invoke lstrcpyA,buffer2,filename
invoke lstrcatA, buffer2,esi
invoke SendDlgItemMessageA, [hwnd], fileslistbox, LB_ADDSTRING, 0, buffer2
.endif

jmp .fileloop2
.list_end2:
add esp, 0x140

invoke  MessageBoxA, 0, [array], 0, 0
invoke  MessageBoxA, 0, [array+4], 0, 0
invoke  MessageBoxA, 0, [array+8], 0, 0

ret     ; Not really needed


        jmp     .finish 

    


what I am trying to do is i give the user a folder dialog and they pick a folder, then I organize the string so it will show full path. then I run some code to get all files in listbox and all folders in an array. this is the part that has the problem. I have it saying if ecx = 0 its a folder if not its a file. this works.

this is the part with the problem

Code:

.if ecx = 0
    invoke lstrcpyA,buffer2,filename
    invoke lstrcatA,buffer2,esi
    mov ecx,[count]
    mov eax,4
    mul ecx
    mov dword [array+eax],buffer2
    invoke  MessageBoxA, 0, [array+eax], 0, 0
    invoke SendDlgItemMessageA, [hwnd], folderslistbox, LB_ADDSTRING, 0, buffer2
    mov ecx,0
    inc [count]
    .endif

    


I format my string and inc the count so it goes 4,8 etc and move the string into the array. I put the messagebox right after that to see hat it is moving into the array and it is moving the folder into the array. the problem is at the end of the code where I do this to check my array since were finished

Code:

invoke  MessageBoxA, 0, [array], 0, 0
    invoke  MessageBoxA, 0, [array+4], 0, 0
    invoke  MessageBoxA, 0, [array+8], 0, 0

    


all of the sudden all three of these message boxes give me the last file in the folder. I don't get how this is possibly because I never write a file to the array and why did it overwrite the folders I put into the arrays early.

so my question is how can I get my arrays to not get overwritten and hold the folders I put into them?

thank you so much.
Post 09 Apr 2014, 23:22
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17716
Location: In your JS exploiting you and your system
revolution
Unconstrained file lengths are probably overwriting your name buffers into the next string.

Ideally you would have a separate memory allocation for each filename based upon the filename's length. Basically, you can't just assume all filenames are less than X characters long. In Windows, UNC paths can be as much as 32768 characters. And in UNICODE mode each character is 2 bytes, giving 65536 bytes per filename.

Also, limiting your selection to four files in the source is probably going to hurt you at some point in the future. Instead consider allocating the string pointer buffer dynamically based upon the actual number of files in the list.
Post 09 Apr 2014, 23:40
View user's profile Send private message Visit poster's website Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1419
Location: Toronto, Canada
AsmGuru62
Also, no need to move the strings into array at run-time.
You can do it like that:
Code:
array dd str0,str1,str2,str3
    

However, you do need a dynamic memory allocation here to provide a list of strings, where the count of strings is not known beforehand.
Post 10 Apr 2014, 00:22
View user's profile Send private message Send e-mail Reply with quote
patchariadog



Joined: 24 Mar 2013
Posts: 94
patchariadog
I am sorry but I am kind of new at assembly, so how do I fix the Unconstrained file lengths. I will fix the 4 files because it is for a file shredding program so yeah their will be a lot more then 4, but how do I get them not to overwrite themselves?

thanks
Post 10 Apr 2014, 01:01
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17716
Location: In your JS exploiting you and your system
revolution
patchariadog wrote:
... how do I fix the Unconstrained file lengths ...
Count the number of files first and then allocate the array. And don't statically allocate memory for the names either. As each file is added allocate enough memory for the name and anything else you add to the name (like the path or whatever) and then add the name pointer to the array.
Post 10 Apr 2014, 01:18
View user's profile Send private message Visit poster's website Reply with quote
patchariadog



Joined: 24 Mar 2013
Posts: 94
patchariadog
okay I get it now. thank you revolution and AsmGuru62 for your help.
Post 10 Apr 2014, 01:23
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
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
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.