[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#844038: ocamlnet: FTBFS on hurd-i386: PATH_MAX



Hello,

Samuel Thibault, on Sat 12 Nov 2016 00:34:48 +0100, wrote:
> The attached patch avoids using by by just reading the symlink
> length, and adjusting the size in case the symlink length increased in
> between through really bad concurrency luck.

Sorry, I should have really tested it, here is a fixed patch.

Samuel
--- ./src/netsys/netsys_c.c.original	2016-11-11 23:14:29.000000000 +0000
+++ ./src/netsys/netsys_c.c	2016-11-11 23:25:42.000000000 +0000
@@ -607,12 +607,32 @@
 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);
+  char *buffer;
+  int buflen, len;
+  struct stat sb;
+  value ret;
+  if (lstat(String_val(path), &sb) != -1) {
+    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);
+  return ret;
 #else
     invalid_argument("Netsys_posix.readlinkat not available");
 #endif

Reply to: