---------------------------------------------------------------
                      [BUG/PRB.] VFP 9.0 FIX - LEFTC() FUNCTION
                               January 2024
                 ---------------------------------------------------------------
                                     CCB



1. BUG:

     In vfp9 (and vfp7, vfp8), the LEFTC(cExpression, nCharacters) function returns cExpression if nCharacters < 0,
     now the LEFTC(cExpression, nCharacters) function returns an empty string if nCharacters < 0.

     The bug occurs in vfp7, vfp8 and vfp9, it does not occur in vfp6.

     There is a test program:

     *PROC testleftcfunction

     SET STEP OFF
     SET ECHO OFF
     SET DEBUG OFF
     SET ESCAPE OFF
     SET TALK OFF
     SET SAFETY OFF

     ?LEFT("abc",-5) && it displays "".
     ?LEFTC("abc",-5) && it displays "abc" in vfp9 (and vfp7, vfp8), it displays "" in vfpa (and vfp6).
     WAIT

     RETURN

     * END OF PROC TESTLEFTCFUNCTION.


2. CAUSE:

     There are some BUGs in the following code.


3. RESOLUTION:

     We can write some code to fix the BUG.

     Fun5056ee :: ; proc near
             push ecx                                                        ;0x005056ee :        51
             push ebp                                                        ;0x005056ef :        55
             push esi                                                        ;0x005056f0 :        56
             push edi                                                        ;0x005056f1 :        57
             call Fun420d8c                                                  ;0x005056f2 :        e895b6f1ff
             mov esi , eax                                                   ;0x005056f7 :        8bf0
             lea ebp ,  dword ptr [ esi + 44 ]                               ;0x005056f9 :        8d6e2c
             push ebp                                                        ;0x005056fc :        55
             lea edi ,  dword ptr [ esp + 16 ]                               ;0x005056fd :        8d7c2410
             call Fun420dd3                                                  ;0x00505701 :        e8cdb6f1ff
             test eax , eax                                                  ;0x00505706 :        85c0
             jne Label505757                                                 ;0x00505708 :        754d
             push ebx                                                        ;0x0050570a :        53
             mov eax , esi                                                   ;0x0050570b :        8bc6
             call Fun42371d                                                  ;0x0050570d :        e80be0f1ff
             mov edi , ebp                                                   ;0x00505712 :        8bfd
             call Fun529a6e                                                  ;0x00505714 :        e855430200
             cmp  byte ptr [ ebp + 0 ] , 049h                                ;0x00505719 :        807d0049
             mov bl , 01h                                                    ;0x0050571d :        b301
             jne Label59104d                                                 ;0x0050571f :        0f8528b90800

     Label505725 ::
             mov  eax , dword ptr [ esp + 24 ]                               ;0x00505725 :        8b442418
             test eax , eax                                                  ;0x00505729 :        85c0
             jne Label59107b                                                 ;0x0050572b :        0f854ab90800

     Label505731 ::
             mov  ebx , dword ptr [ esi + 56 ]                               ;0x00505731 :        8b5e38
             test ebx , ebx                                                  ;0x00505734 :        85db
             jl Label4500ab                                                  ;0x00505736 :        0f8c6fa9f4ff
             mov  eax , dword ptr [ esi + 8 ]                                ;0x0050573c :        8b4608
             cmp ebx , eax                                                   ;0x0050573f :        3bd8
             jg Label50177e                                                  ;0x00505741 :        0f8f37c0ffff

     Label505747 ::
             mov  eax , dword ptr [ esp + 16 ]                               ;0x00505747 :        8b442410
             test eax , eax                                                  ;0x0050574b :        85c0
             mov  dword ptr [ esi + 8 ] , ebx                                ;0x0050574d :        895e08
             pop ebx                                                         ;0x00505750 :        5b
             jne Label5910bd                                                 ;0x00505751 :        0f8566b90800

     Label505757 ::
             pop edi                                                         ;0x00505757 :        5f
             pop esi                                                         ;0x00505758 :        5e
             pop ebp                                                         ;0x00505759 :        5d
             pop ecx                                                         ;0x0050575a :        59
             ret 04h                                                         ;0x0050575b :        c20400

     Label59107b ::
             mov  eax , dword ptr [ esi + 56 ]                               ;0x0059107b :        8b4638
             mov  ecx , dword ptr [ esi + 8 ]                                ;0x0059107e :        8b4e08
             cmp eax , ecx                                                   ;0x00591081 :        3bc1


     ;
     ;                 ----------------------------------------------------
     ;                      VFP 9.0 FIX - LEFTC() FUNCTION
     ;                                  June 2022
     ;                 ----------------------------------------------------
     ;                                     CCB
     ;
     ; In vfp9, the LEFTC(cExpression, nCharacters) function returns cExpression if nCharacters < 0,
     ; now the LEFTC(cExpression, nCharacters) function returns an empty string if nCharacters < 0.
     ;
     ; 2022/6/27, by ccb
     ;

     ;         jnb Label591087                                                 ;0x00591083 :        7302
             jnl Label591087                                                 ;0x00591083 :        7302


             mov ecx , eax                                                   ;0x00591085 :        8bc8

     Label591087 ::
             mov eax ,  dword ptr [ Data9370f4 ]                             ;0x00591087 :        a1f4709300
             test  byte ptr ah , 010h                                        ;0x0059108c :        f6c410
             je Label5910b3                                                  ;0x0059108f :        7422
             xor eax , eax                                                   ;0x00591091 :        33c0
             test ecx , ecx                                                  ;0x00591093 :        85c9
             jle Label5910b5                                                 ;0x00591095 :        7e1e
             mov  edx , dword ptr [ esi + 32 ]                               ;0x00591097 :        8b5620
             mov  edx , dword ptr [edx]                                      ;0x0059109a :        8b12

     Label59109c ::
             movzx edi ,byte ptr [ eax + edx ]                               ;0x0059109c :        0fb63c10
             test  byte ptr [ edi + offset Data937120 ] , bl                          ;0x005910a0 :        849f20719300
             je Label5910ad                                                  ;0x005910a6 :        7405
             add eax , 02h                                                   ;0x005910a8 :        83c002
             jmp Label5910ae                                                 ;0x005910ab :        eb01

     Label5910ad ::
             inc eax                                                         ;0x005910ad :        40

     Label5910ae ::
             dec ecx                                                         ;0x005910ae :        49
             jne Label59109c                                                 ;0x005910af :        75eb
             jmp Label5910b5                                                 ;0x005910b1 :        eb02

     Label5910b3 ::
             mov eax , ecx                                                   ;0x005910b3 :        8bc1

     Label5910b5 ::
             mov  dword ptr [ esi + 56 ] , eax                               ;0x005910b5 :        894638
             jmp Label505731                                                 ;0x005910b8 :        e97446f7ff


4. APPLIES TO:

     VFP 7.0.0.9262
     VFP 7.0.0.9465 (SP1)

     VFP 8.0.0.2521
     VFP 8.0.0.3117 (SP1)

     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


6. OTHER:

     For reference only, there is no guarantees.

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