Project

General

Profile

Actions

Feature #13586

closed

getcwd() should accept a 0 length argument

Added by Rich Lowe 3 months ago. Updated 2 months ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
lib - userland libraries
Start date:
Due date:
% Done:

100%

Estimated time:
Difficulty:
Bite-size
Tags:
Gerrit CR:

Description

As noted here: https://github.com/libarchive/libarchive/issues/1498
everyone else accepts a 0 argument for the buffer size, and allocates as much as necessary. We should too.


Related issues

Related to illumos gate - Feature #4933: getcwd(3c) could be more usefulDuplicate2014-06-21

Actions
Actions #1

Updated by Andy Fiddaman 3 months ago

  • Status changed from New to In Progress
  • Assignee set to Andy Fiddaman
Actions #2

Updated by Andy Fiddaman 3 months ago

  • Related to Feature #4933: getcwd(3c) could be more useful added
Actions #3

Updated by Electric Monk 3 months ago

  • Gerrit CR set to 1296
Actions #4

Updated by Andy Fiddaman 3 months ago

  • Subject changed from getcwd() should accept a 0 length argument to getcwd() should accept a 0 length argument
Actions #5

Updated by Andy Fiddaman 2 months ago

I tested this in conjunction with the fix for #9620 because otherwise I hit that bug when using very long paths.

With that fix in place, for a range of path lengths up to 64K, I was able to call getcwd(NULL, 0) and get back the full path.
(Without the fix, this worked for some path lengths, and failed for others, however the realloc() loop worked as expected until the syscall returned ERANGE early).

Actions #6

Updated by Andy Fiddaman 2 months ago

Some more testing notes:

For a fairly standard short path:

bloody# echo ${#PWD}
25
bloody# ./libc.d -c "$HOME/getcwd/getcwd 50" 
dtrace: script './libc.d' matched 10 probes
Calling getcwd(NULL, 50)
Got strlen: 25
dtrace: pid 630 has exited
CPU     ID                    FUNCTION:NAME
  9  68528                     malloc:entry                50

bloody# ./libc.d -c "$HOME/getcwd/getcwd 0" 
dtrace: script './libc.d' matched 10 probes
Calling getcwd(NULL, 0)
Got strlen: 25
dtrace: pid 633 has exited
CPU     ID                    FUNCTION:NAME
 12  68520                   reallocf:entry              1024
 12  68522                    realloc:entry                26

A 30K path:

bloody# echo ${#PWD}
30052
bloody# ~/getcwd/libc.d -c "$HOME/getcwd/getcwd" 
dtrace: script '/data/omnios-build/getcwd/libc.d' matched 10 probes
Calling getcwd(NULL, 0)
Got strlen: 30052
dtrace: pid 637 has exited
CPU     ID                    FUNCTION:NAME
 10  68520                   reallocf:entry              1024
 10  68520                   reallocf:entry              2048
 10  68520                   reallocf:entry              4096
 10  68520                   reallocf:entry              8192
 10  68520                   reallocf:entry             16384
 10  68520                   reallocf:entry             32768
 10  68522                    realloc:entry             30053

A path that is bigger than the limit in libc's getcwd()

bloody# echo ${#PWD}
140023
bloody# ~/getcwd/libc.d -c "$HOME/getcwd/getcwd" 
dtrace: script '/data/omnios-build/getcwd/libc.d' matched 10 probes
Calling getcwd(NULL, 0)
Err: 34 (Result too large)
dtrace: pid 896 has exited
CPU     ID                    FUNCTION:NAME
  9  68520                   reallocf:entry              1024
  9  68520                   reallocf:entry              2048
  9  68520                   reallocf:entry              4096
  9  68520                   reallocf:entry              8192
  9  68520                   reallocf:entry             16384
  9  68520                   reallocf:entry             32768
  9  68520                   reallocf:entry             65536
  9  68520                   reallocf:entry            131072
Actions #7

Updated by Electric Monk 2 months ago

  • Status changed from In Progress to Closed
  • % Done changed from 0 to 100

git commit aa15372140b6b509a26742fd85fe78dd77d9a642

commit  aa15372140b6b509a26742fd85fe78dd77d9a642
Author: Andy Fiddaman <omnios@citrus-it.co.uk>
Date:   2021-03-16T19:20:11.000Z

    13586 getcwd() should accept a 0 length argument
    Reviewed by: Jason King <jason.king@joyent.com>
    Reviewed by: Dan McDonald <danmcd@joyent.com>
    Reviewed by: Gordon Ross <gordon.w.ross@gmail.com>
    Reviewed by: Robert Mustacchi <rm@fingolfin.org>
    Approved by: Rich Lowe <richlowe@richlowe.net>

Actions #8

Updated by Electric Monk 2 months ago

git commit 6edc7986d8c0034d072afac8b25477b983bf8f55

commit  6edc7986d8c0034d072afac8b25477b983bf8f55
Author: Andy Fiddaman <omnios@citrus-it.co.uk>
Date:   2021-03-16T23:45:24.000Z

    13586 getcwd() should accept a 0 length argument (fix mandoc)
    Approved by: Dan McDonald <danmcd@joyent.com>

Actions

Also available in: Atom PDF