--------------------------------------------------------------------------
                      VFP 9.0 FIX - CHECK AVAILABLE MEMORY FOR CREATING A BINARY INDEX
                                September 2020
                 --------------------------------------------------------------------------
                                     CCB



1. BUG:

     If we enable checking available memory for creating a binary index in Visual FoxPro Advanced,
     but there is not enough memory to create the binary index,
     it will occur an error: There is not enough memory to complete this operation (Error 43).
     It is the startup default for Visual FoxPro Advanced.

     If we disable checking available memory for creating a binary index in Visual FoxPro Advanced,
     but there is not enough memory to create the binary index,
     it will enter an infinite loop, it is the same as Visual FoxPro 9.0 and earlier versions.

     There is the bug for creating a binary index in Visual FoxPro 9.0.
     There is no the bug for creating a non-binary index in Visual FoxPro 9.0.
     There is no the bug in Visual FoxPro 8.0 and earlier versions.


2. CAUSE:

     To create a binary index in Visual FoxPro,
     it need 16 bytes per record.

     If RECCOUNT()=90000000,
     it need 16 * 90000000 = 1440000000 bytes < 1466695680 bytes (the SYS(3050,1) default setting),
     so it can create the binary index.

     If RECCOUNT()=100000000,
     it need 16 * 100000000 = 1600000000 bytes > 1466695680 bytes (the SYS(3050,1) default setting),
     so it can't create the binary index.


3. RESOLUTION:

     We can write some code to fix the BUG.

     Fun4203bc :: ; proc near
             mov eax ,  dword ptr [ Data937584 ]                             ;0x004203bc :        a184759300
             mov  ecx , dword ptr [ Data9370d4 ]                             ;0x004203c1 :        8b0dd4709300
             push ebx                                                        ;0x004203c7 :        53
             push ebp                                                        ;0x004203c8 :        55
             mov  ebp , dword ptr [ esp + 12 ]                               ;0x004203c9 :        8b6c240c
             mov  ebx , dword ptr [ ebp + 24 ]                               ;0x004203cd :        8b5d18
             push esi                                                        ;0x004203d0 :        56
             sub  eax , dword ptr [ Data9392ac ]                             ;0x004203d1 :        2b05ac929300
             add ebx , 04h                                                   ;0x004203d7 :        83c304
             cmp ecx , eax                                                   ;0x004203da :        3bc8
             mov  dword ptr [ esp + 16 ] , 00h                               ;0x004203dc :        c744241000000000
             jb Label5a5bcb                                                  ;0x004203e4 :        0f82e1571800
             sub ecx , eax                                                   ;0x004203ea :        2bc8

     Label4203ec ::
             mov eax ,  dword ptr [ Data937784 ]                             ;0x004203ec :        a184779300
             add eax , ecx                                                   ;0x004203f1 :        03c1
             mov ecx , eax                                                   ;0x004203f3 :        8bc8
             shr ecx , 1                                                     ;0x004203f5 :        d1e9
             cmp eax , 020000h                                               ;0x004203f7 :        3d00000200
             jbe Label420485                                                 ;0x004203fc :        0f8683000000
             lea esi ,  dword ptr [ eax + 0FFFE0000h ]                       ;0x00420402 :        8db00000feff

     Label420408 ::
             cmp ecx , esi                                                   ;0x00420408 :        3bce
             ja Label5a5bd2                                                  ;0x0042040a :        0f87c2571800

     Label420410 ::
             test esi , esi                                                  ;0x00420410 :        85f6
             je Label5a5bd9                                                  ;0x00420412 :        0f84c1571800
             mov  ecx , dword ptr [ ebp + 28 ]                               ;0x00420418 :        8b4d1c
             mov eax , ecx                                                   ;0x0042041b :        8bc1
             imul eax , ebx                                                  ;0x0042041d :        0fafc3
             cmp eax , 01h                                                   ;0x00420420 :        83f801
             jbe Label5a5be3                                                 ;0x00420423 :        0f86ba571800

     Label420429 ::


     ;
     ;                 --------------------------------------------------------------------------
     ;                      VFP 9.0 FIX - CHECK AVAILABLE MEMORY FOR CREATING A BINARY INDEX
     ;                                September 2020
     ;                 --------------------------------------------------------------------------
     ;                                     CCB
     ;
     ; Check available memory for creating a binary index.
     ;
     ; 2020/9/21, by ccb
     ;

             cmp dword ptr vfpa_sys9036_data,00h
             je Label42042a
             test byte ptr [ ebp + 66 ] , 02h
             je Label42042a
             cmp eax , esi
             ja Label5a5bd9


     Label42042a ::
             dec eax                                                         ;0x00420429 :        48
             xor edx , edx                                                   ;0x0042042a :        33d2
             div esi                                                         ;0x0042042c :        f7f6
             push edi                                                        ;0x0042042e :        57
             pushd 00h                                                       ;0x0042042f :        6a00
             mov esi , eax                                                   ;0x00420431 :        8bf0
             mov eax , ecx                                                   ;0x00420433 :        8bc1
             inc esi                                                         ;0x00420435 :        46
             cdq                                                             ;0x00420436 :        99
             idiv esi                                                        ;0x00420437 :        f7fe
             mov esi , eax                                                   ;0x00420439 :        8bf0
             add esi , 02h                                                   ;0x0042043b :        83c602
             mov edi , esi                                                   ;0x0042043e :        8bfe
             imul edi , ebx                                                  ;0x00420440 :        0faffb
             call Fun42db6a                                                  ;0x00420443 :        e822d70000
             test eax , eax                                                  ;0x00420448 :        85c0
             mov  dword ptr [ ebp + 36 ] , eax                               ;0x0042044a :        894524
             je Label5a5bed                                                  ;0x0042044d :        0f849a571800

     Label420453 ::
             mov  ecx , dword ptr [ ebp + 36 ]                               ;0x00420453 :        8b4d24
             xor eax , eax                                                   ;0x00420456 :        33c0
             test esi , esi                                                  ;0x00420458 :        85f6
             pop edi                                                         ;0x0042045a :        5f
             jle Label5a5c25                                                 ;0x0042045b :        0f8ec4571800
             mov eax , esi                                                   ;0x00420461 :        8bc6
             nop                                                             ;0x00420463 :        90

     Label420464 ::
             mov  edx , dword ptr [ esp + 16 ]                               ;0x00420464 :        8b542410
             mov  dword ptr [ecx] , edx                                      ;0x00420468 :        8911
             mov  dword ptr [ esp + 16 ] , ecx                               ;0x0042046a :        894c2410
             add ecx , ebx                                                   ;0x0042046e :        03cb
             dec eax                                                         ;0x00420470 :        48
             jne Label420464                                                 ;0x00420471 :        75f1
             mov  eax , dword ptr [ esp + 16 ]                               ;0x00420473 :        8b442410
             mov  ecx , dword ptr [ esp + 20 ]                               ;0x00420477 :        8b4c2414
             mov  dword ptr [ecx] , eax                                      ;0x0042047b :        8901
             mov eax , esi                                                   ;0x0042047d :        8bc6
             pop esi                                                         ;0x0042047f :        5e
             pop ebp                                                         ;0x00420480 :        5d
             pop ebx                                                         ;0x00420481 :        5b
             ret 08h                                                         ;0x00420482 :        c20800


4. APPLIES TO:

     VFP 9.0.0.2412
     VFP 9.0.0.3504 (SP1)
     VFP 9.0.0.4611 (SP2)
     VFP 9.0.0.5015 (SP2)
     VFP 9.0.0.5411 (SP2)
     VFP 9.0.0.5721 (SP2)
     VFP 9.0.0.5815 (SP2)
     VFP 9.0.0.6303 (SP2)
     VFP 9.0.0.6602 (SP2)
     VFP 9.0.0.7423 (SP2)

     The bug has been fixed in VFP Advanced.


5. REFERENCE WEBSITES:

     1, baiyujia.com:
     http://www.baiyujia.com
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix125.asp


6. OTHER:

     For reference only, there is no guarantees.

     Any questions or suggestions, please send me an email at ccb2000@163.com.