tldr-pages / tldr-c-client

C command-line client for tldr pages
MIT License
293 stars 50 forks source link

'tldr --list' segfaults on macOS #71

Closed zcutlip closed 2 years ago

zcutlip commented 2 years ago

Reproduction Rate

3 / 3 times

Steps to Reproduce

run tldr --list

Result

Segmentation fault

Expected Result

A list of all entires in the database

Additional Information

tldr: 1.4.2 macOS 12.1 (12C52)

tldr --list results in a segfault. tldr -l results in tldr: option '-l' is ambiguous (which may be a separate bug)

I haven't reset the cache or any other artifacts in case they are useful in reproducing.

Full crash report attached. Translated report from Console.app below:

-------------------------------------
Translated Report (Full Report Below)
-------------------------------------

Process:               tldr [39126]
Path:                  /usr/local/Cellar/tldr/1.4.2/bin/tldr
Identifier:            tldr
Version:               ???
Code Type:             X86-64 (Native)
Parent Process:        zsh [36476]
Responsible:           iTerm2 [651]
User ID:               503

Date/Time:             2021-12-21 19:48:26.8398 -0800
OS Version:            macOS 12.1 (21C52)
Report Version:        12
Bridge OS Version:     6.1 (19P647)
Anonymous UUID:        11B31165-9192-89A0-D780-9093191C32D6

Sleep/Wake UUID:       12E9DD6D-D639-44C3-9730-54F7087A6E1C

Time Awake Since Boot: 6900 seconds
Time Since Wake:       1745 seconds

System Integrity Protection: enabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x0000000000000040
Exception Codes:       0x0000000000000001, 0x0000000000000040
Exception Note:        EXC_CORPSE_NOTIFY

Termination Reason:    Namespace SIGNAL, Code 11 Segmentation fault: 11
Terminating Process:   exc handler [39126]

VM Region Info: 0x40 is not in any region.  Bytes before following region: 4417503168
      REGION TYPE                    START - END         [ VSIZE] PRT/MAX SHRMOD  REGION DETAIL
      UNUSED SPACE AT START
--->  
      __TEXT                      1074dc000-1074e4000    [   32K] r-x/r-x SM=COW  ....4.2/bin/tldr

Thread 0 Crashed::  Dispatch queue: com.apple.main-thread
0   libsystem_pthread.dylib             0x7ff80be2d94e pthread_mutex_lock + 4
1   libsystem_c.dylib                   0x7ff80bd04a7d readdir$INODE64 + 22
2   tldr                                   0x1074e2dbd parse_tldrlist + 228
3   tldr                                   0x1074e2c87 print_tldrlist + 361
4   tldr                                   0x1074e3150 main + 703
5   dyld                                   0x115d434fe start + 462

Thread 0 crashed with X86 Thread State (64-bit):
  rax: 0x0000000000000000  rbx: 0x0000000000000000  rcx: 0x00007ff80bdf64fe  rdx: 0xffffffffffffffff
  rdi: 0x0000000000000040  rsi: 0x0000000001100004  rbp: 0x00007ff7b8a21740  rsp: 0x00007ff7b8a21740
   r8: 0x00007ff84d65b488   r9: 0x0000000000000000  r10: 0x0000000115dbe6ac  r11: 0x0000000000000247
  r12: 0x0000000000000000  r13: 0x00000001074e3c0c  r14: 0x0000000000000040  r15: 0x00000001074e3a53
  rip: 0x00007ff80be2d94e  rfl: 0x0000000000010206  cr2: 0x0000000000000040

Logical CPU:     2
Error Code:      0x00000004 (no mapping for user data read)
Trap Number:     14

Thread 0 instruction stream:
  48 89 f3 49 89 fe 48 8d-3d e9 06 83 41 be 00 00  H..I..H.=...A...
  05 00 e8 4f 80 00 00 b8-00 01 00 00 48 8d 0d df  ...O........H...
  06 83 41 48 83 3c c1 00-74 12 48 ff c0 48 3d 00  ..AH.<..t.H..H=.
  03 00 00 75 ee bb 23 00-00 00 eb 0c 48 f7 d3 48  ...u..#.....H..H
  89 1c c1 49 89 06 31 db-48 8d 3d a7 06 83 41 e8  ...I..1.H.=...A.
  18 80 00 00 89 d8 5b 41-5e 5d c3 90 55 48 89 e5  ......[A^]..UH..
 [48]81 3f 5a 54 55 4d 75-73 8b 47 0c 89 c1 81 e1  H.?ZTUMus.G..... <==
  c0 01 00 00 83 f9 40 74-6b 0f ba e0 0e 72 6d 48  ......@tk....rmH
  8d 57 27 48 83 e2 f8 48-8b 0a f6 c1 02 75 45 4c  .W'H...H.....uEL
  8d 57 1f 49 83 e2 f8 65-4c 8b 04 25 f8 ff ff ff  .W.I...eL..%....
  49 b9 00 00 00 00 ff ff-ff ff 48 89 ce f6 c1 02  I.........H.....
  75 22 4c 21 ce 89 c8 48-09 f0 83 c9 02 48 09 f1  u"L!...H.....H..

Binary Images:
    0x7ff80be2c000 -     0x7ff80be37fff libsystem_pthread.dylib (*) <6c7561b4-4b92-3f45-921e-abe669299844> /usr/lib/system/libsystem_pthread.dylib
    0x7ff80bcfd000 -     0x7ff80bd85fff libsystem_c.dylib (*) <e58814cc-dcb7-35a5-badc-e367ed3ac207> /usr/lib/system/libsystem_c.dylib
       0x1074dc000 -        0x1074e3fff tldr (*) <5e171f9c-478a-3c66-9fbb-08c96eac8e7b> /usr/local/Cellar/tldr/1.4.2/bin/tldr
       0x115d3e000 -        0x115da9fff dyld (*) <cef5a27a-d50b-3020-af03-1734b19bc8c5> /usr/lib/dyld
               0x0 - 0xffffffffffffffff ??? (*) <00000000-0000-0000-0000-000000000000> ???

External Modification Summary:
  Calls made by other processes targeting this process:
    task_for_pid: 0
    thread_create: 0
    thread_set_state: 0
  Calls made by this process:
    task_for_pid: 0
    thread_create: 0
    thread_set_state: 0
  Calls made by all processes on this machine:
    task_for_pid: 0
    thread_create: 0
    thread_set_state: 0

VM Region Summary:
ReadOnly portion of Libraries: Total=398.4M resident=0K(0%) swapped_out_or_unallocated=398.4M(100%)
Writable regions: Total=531.3M written=0K(0%) resident=0K(0%) swapped_out=0K(0%) unallocated=531.3M(100%)

                                VIRTUAL   REGION 
REGION TYPE                        SIZE    COUNT (non-coalesced) 
===========                     =======  ======= 
Activity Tracing                   256K        1 
Kernel Alloc Once                    8K        1 
MALLOC                           139.1M       12 
MALLOC guard page                   16K        4 
MALLOC_NANO (reserved)           384.0M        1         reserved VM address space (unallocated)
STACK GUARD                       56.0M        1 
Stack                             8192K        1 
VM_ALLOCATE                          8K        2 
__DATA                            2402K      136 
__DATA_CONST                      5238K       89 
__DATA_DIRTY                       209K       51 
__LINKEDIT                       312.5M        8 
__OBJC_RO                         81.8M        1 
__OBJC_RW                         3136K        2 
__TEXT                            85.9M      150 
__UNICODE                          588K        1 
dyld private memory               1024K        1 
shared memory                       12K        2 
===========                     =======  ======= 
TOTAL                              1.1G      464 
TOTAL, minus reserved VM space   695.9M      464 

tldr-2021-12-21-194826.ips.zip

zcutlip commented 2 years ago

The problem appears to be it looking for pages under ~/.tldrc/tldr/pages/osx. I don't have that directory:

$ ls -1 ~/.tldrc
date
tldr-main/

Under ~/.tldrc/tldr-main/pages, I have the various platforms, including 'osx'.

So there appears to be two bugs:

  1. The data directory that gets created and populated is different than the one expected when listing entries
  2. when calling opendir() in parse_tldrlist() (parser.c:275), it's not checked that the call was successful. In this case opendir() returns NULL which results in the segfault later.

For (1), it looks like TLDR_DIR and TLDR_EXT are getting mixed up at various places, but I wasn't able to sort out what was intended

For (2), the following prevents the segfault, although listing still fails:

parse_tldrlist(char const *path, char const *platform)
{
...
    directory = opendir(fullpath);
    if(!directory)
    {
        perror("opendir()");
        return 1;
    }
...
    return 0;
}
zcutlip commented 2 years ago

Creating a symlink is a workaround:

$ ln -s ~/.tldrc/tldr-main ~/.tldrc/tldr
zcutlip commented 2 years ago

Ah, looks like this PR introduced the regression: https://github.com/tldr-pages/tldr-c-client/pull/66