Bug 254120

Summary: Linuxulator: getcwd returns ENOMEM instead of ERANGE
Product: Base System Reporter: Alex S <iwtcex>
Component: kernAssignee: Edward Tomasz Napierala <trasz>
Status: Closed FIXED    
Severity: Affects Only Me CC: emaste, trasz
Priority: ---    
Version: Unspecified   
Hardware: Any   
OS: Any   
Bug Depends on:    
Bug Blocks: 247219    

Description Alex S 2021-03-08 03:06:44 UTC
% cat getcwd.c 
#include <assert.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>

int main() {

  char buf[3];
  assert(getcwd(buf, sizeof(buf)) == NULL);
  if (errno != ERANGE) {    
    perror("getcwd");
  }

  return 0;
}
% pwd | wc -c
      29
% /compat/linux/bin/cc getcwd.c -o getcwd
% ./getcwd 
getcwd: Cannot allocate memory

On the Linux side getcwd is clearly documented to return ERANGE and
at least Mono actually expects it:
https://github.com/mono/mono/blob/da11592cbea4269971f4b1f9624769a85cc10660/mono/eglib/gfile-posix.c#L175.

This is mostly relevant for FNA/MonoGame applications.
Comment 1 Edward Tomasz Napierala freebsd_committer freebsd_triage 2021-03-11 12:21:35 UTC
https://reviews.freebsd.org/D29217
Comment 2 commit-hook freebsd_committer freebsd_triage 2021-03-12 15:34:53 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=0dfbdd9fc269f0438ffcc31632d35234a90584ad

commit 0dfbdd9fc269f0438ffcc31632d35234a90584ad
Author:     Edward Tomasz Napierala <trasz@FreeBSD.org>
AuthorDate: 2021-03-12 15:31:37 +0000
Commit:     Edward Tomasz Napierala <trasz@FreeBSD.org>
CommitDate: 2021-03-12 15:31:45 +0000

    linux(4): make getcwd(2) return ERANGE instead of ENOMEM

    For native FreeBSD binaries, the return value from __getcwd(2)
    doesn't really matter, as the libc wrapper takes over and returns
    the proper errno.

    PR:             kern/254120
    Reported By:    Alex S <iwtcex@gmail.com>
    Reviewed By:    kib
    Sponsored By:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D29217

 sys/compat/linux/linux_getcwd.c | 2 ++
 1 file changed, 2 insertions(+)
Comment 3 commit-hook freebsd_committer freebsd_triage 2021-03-15 13:40:55 UTC
A commit in branch stable/13 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=ab1a91d95872e59db3d476be4fefb0b58df3afc4

commit ab1a91d95872e59db3d476be4fefb0b58df3afc4
Author:     Edward Tomasz Napierala <trasz@FreeBSD.org>
AuthorDate: 2021-03-12 15:31:37 +0000
Commit:     Edward Tomasz Napierala <trasz@FreeBSD.org>
CommitDate: 2021-03-15 13:00:17 +0000

    linux(4): make getcwd(2) return ERANGE instead of ENOMEM

    For native FreeBSD binaries, the return value from __getcwd(2)
    doesn't really matter, as the libc wrapper takes over and returns
    the proper errno.

    PR:             kern/254120
    Reported By:    Alex S <iwtcex@gmail.com>
    Reviewed By:    kib
    Sponsored By:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D29217

    (cherry picked from commit 0dfbdd9fc269f0438ffcc31632d35234a90584ad)

 sys/compat/linux/linux_getcwd.c | 2 ++
 1 file changed, 2 insertions(+)
Comment 4 commit-hook freebsd_committer freebsd_triage 2021-03-16 18:39:10 UTC
A commit in branch releng/13.0 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=56dd03e0f2b17e703ba8daad8bc9ab21a761a461

commit 56dd03e0f2b17e703ba8daad8bc9ab21a761a461
Author:     Edward Tomasz Napierala <trasz@FreeBSD.org>
AuthorDate: 2021-03-12 15:31:37 +0000
Commit:     Edward Tomasz Napierala <trasz@FreeBSD.org>
CommitDate: 2021-03-16 18:37:57 +0000

    linux(4): make getcwd(2) return ERANGE instead of ENOMEM

    For native FreeBSD binaries, the return value from __getcwd(2)
    doesn't really matter, as the libc wrapper takes over and returns
    the proper errno.

    Approved by:    re (gjb)
    PR:             kern/254120
    Reported By:    Alex S <iwtcex@gmail.com>
    Reviewed By:    kib
    Sponsored By:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D29217

    (cherry picked from commit 0dfbdd9fc269f0438ffcc31632d35234a90584ad)
    (cherry picked from commit ab1a91d95872e59db3d476be4fefb0b58df3afc4)

 sys/compat/linux/linux_getcwd.c | 2 ++
 1 file changed, 2 insertions(+)