CSPro crash when using dirlist

Other discussions about CSPro
Forum rules
New release: CSPro 8.0
Post Reply
pierre

CSPro crash when using dirlist

Post by pierre »

Hey guys,
CSPro 6.1 crashes when you try to use the dirlist statement, if the directory is empty.
Is there a statement that allows you to check the number of files in a directory?
Thanks,
Pierre

Code: Select all

	if direxist(tmpStr)then
		ok = dirlist(fileListing,tmpStr,"*.dat",recursive);
		if ok then 
			// 123456789012345678
			// 00_00_00_00000.DAT
			tmpStr = fileListing(1);
			tmpStr = tmpStr[1:length(tmpStr)-18] + "...\n\n";
			do a = 1 while a <= length(fileListing)
				tmpStr = tmpStr + edit("99",a) + ")" + fileListing(a)[length(fileListing(a))-18+1:18] + "\n";
			enddo;
			S_BLOCK = tmpStr;
		endif;
	endif;
josh
Posts: 2399
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: CSPro crash when using dirlist

Post by josh »

Sorry for the delay on replying to this. Got lost between trips.

Looks the crash is not in dirlist but in the string concatenation later on when you do:

Code: Select all

tmpStr = tmpStr[1:length(tmpStr)-18] + "...\n\n";
When the directory is empty, dirlist still returns 1 since it successfully read the empty directory. However in that case length(fileListing) is zero so when you assign tmpStr to fileListing(1) it is pointing to an invalid entry in the list and when you try to concatenate a substring from an invalid string it crashes. The right behavior is to have the call to fileListing(1) fail with an invalid subscript error and we will add that check in the next release. In the meantime you should change your logic to check both the return value of dirlist and the length of the result:

Code: Select all

      if ok and length(fileListing) > 0 then 
Post Reply