File: 0008-Do-not-rely-on-PATH_MAX.patch

package info (click to toggle)
ocamlnet 4.1.8-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 52,232 kB
  • sloc: ml: 151,647; ansic: 11,049; sh: 1,975; makefile: 1,310
file content (56 lines) | stat: -rw-r--r-- 1,625 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
From: Samuel Thibault <sthibault@debian.org>
Date: Thu, 20 Aug 2020 12:32:24 +0200
Subject: Do not rely on PATH_MAX

This fixes compilation on Hurd, which does not define PATH_MAX.

Bug-Debian: https://bugs.debian.org/844038
---
 src/netsys/netsys_c.c | 33 ++++++++++++++++++++++++++++-----
 1 file changed, 28 insertions(+), 5 deletions(-)

diff --git a/src/netsys/netsys_c.c b/src/netsys/netsys_c.c
index 31b7f5d..6ee75da 100644
--- a/src/netsys/netsys_c.c
+++ b/src/netsys/netsys_c.c
@@ -617,12 +617,35 @@ CAMLprim value netsys_mkfifoat(value dirfd, value path, value mode)
 CAMLprim value netsys_readlinkat(value dirfd, value path)
 {
 #ifdef HAVE_AT
-  char buffer[PATH_MAX];
-  int len;
-  len = readlinkat(Int_val(dirfd), String_val(path), buffer, sizeof(buffer)-1);
-  if (len == -1) uerror("readlinkat", path);
+  CAMLparam0();
+  CAMLlocal1(ret);
+  char *buffer;
+  int buflen, len;
+  struct stat sb;
+  if (fstatat(Int_val(dirfd), String_val(path), &sb, AT_SYMLINK_NOFOLLOW) != -1) {
+    if (!S_ISLNK(sb.st_mode))
+      unix_error(EINVAL, "readlinkat", path);
+    buflen = sb.st_size + 1;
+  }
+  else {
+    buflen = 64;
+  }
+  while (1) {
+    buffer = malloc(buflen);
+    len = readlinkat(Int_val(dirfd), String_val(path), buffer, buflen);
+    if (len == -1) {
+      free(buffer);
+      uerror("readlinkat", path);
+    }
+    if (len < buflen)
+      break;
+    free(buffer);
+    buflen *= 2;
+  }
   buffer[len] = '\0';
-  return copy_string(buffer);
+  ret = copy_string(buffer);
+  free(buffer);
+  CAMLreturn(ret);
 #else
     invalid_argument("Netsys_posix.readlinkat not available");
 #endif