Welcome to little lamb

Code » limb » commit 61fdcd5

Add sareadlink0() & sareadlinkat0() to ensure the..

author Olivier Brunel
2023-02-28 19:58:08 UTC
committer Olivier Brunel
2023-03-02 17:41:21 UTC
parent da7d8430ab8af3cf49e1f09ab7fca8568ef271a5

Add sareadlink0() & sareadlinkat0() to ensure the..

..read content is NUL terminated, as it isn't guaranteed by readlink{,at}

doc/sareadlinkat.3.md +21 -5
include/limb/djbunix.h +2 -0
meta/libs/limb +2 -0
src/sareadlink0.c +15 -0
src/sareadlinkat0.c +15 -0

diff --git a/doc/sareadlinkat.3.md b/doc/sareadlinkat.3.md
index 4e665e3..107b053 100644
--- a/doc/sareadlinkat.3.md
+++ b/doc/sareadlinkat.3.md
@@ -11,7 +11,10 @@ sareadlinkat - read the content of a symbolic link
     #include <limb/djbunix.h>
 
 ```pre hl
+int sareadlink0(stralloc *<em>sa</em>, const char * restrict <em>file</em>)
+
 int sareadlinkat(stralloc *<em>sa</em>, int <em>fd</em>, const char * restrict <em>file</em>)
+int sareadlinkat0(stralloc *<em>sa</em>, int <em>fd</em>, const char * restrict <em>file</em>)
 ```
 
 # DESCRIPTION
@@ -27,17 +30,30 @@ the directory associated with the file descriptor `fd`.
 If passed the special value *AT_FDCWD* in the `fd` parameter, the current
 working directory is used.
 
+The `sareadlinkat0`() function is similar to `sareadlinkat`() except that it
+ensures the content placed in `sa` is NUL-terminated.
+
+The `sareadlink0`() function is similar to [sareadlink](3) except that it
+ensures the content placed in `sa` is NUL-terminated.
+
 # RETURN VALUE
 
-Upon successful completion the function returns 0. Otherwise it returns -1 and
-sets `errno` to indicate the error.
+Upon successful completion these functions return 0. Otherwise they return -1
+and set `errno` to indicate the error.
 
 # ERRORS
 
-This function may fail if:
+These functions may fail if:
 
 : *ENOMEM*
 :: Insufficient memory available to grow `sa`
 
-This function may also fail and set `errno` for any of the errors specified for
-the function [`readlinkat`](3).
+The `sareadlinkat`() and `sareadlinkat0`() functions may also fail and set
+`errno` for any of the errors specified for the function [readlinkat](3).
+
+The `sareadlink0`() function may also fail and set `errno` for any of the errors
+specified for the function [readlink](3).
+
+# SEE ALSO
+
+[readlinkat](3), [sareadlink](3)
diff --git a/include/limb/djbunix.h b/include/limb/djbunix.h
index 5358aaf..74d4a80 100644
--- a/include/limb/djbunix.h
+++ b/include/limb/djbunix.h
@@ -11,7 +11,9 @@ extern int rmstar_tmpat(int fd, const char *name, stralloc *sa);
 extern int rmstar_in_tmpat(int fd, stralloc *sa, size_t offset);
 
 extern int salsat(int fd, const char *name, stralloc *sa, size_t *maxlen);
+extern int sareadlink0(stralloc *sa, const char * restrict file);
 extern int sareadlinkat(stralloc *sa, int fd, const char * restrict file);
+extern int sareadlinkat0(stralloc *sa, int fd, const char * restrict file);
 
 #define salst(sa,name,maxlen)       sals(name, sa,maxlen)
 #define salstat(sa,fd,name,maxlen)  salsat(fd, name, sa, maxlen)
diff --git a/meta/libs/limb b/meta/libs/limb
index 6a447d2..6398931 100644
--- a/meta/libs/limb
+++ b/meta/libs/limb
@@ -15,7 +15,9 @@ obj/rmstarat.o
 obj/rmstar_in_tmpat.o
 obj/rmstar_tmpat.o
 obj/salsat.o
+obj/sareadlink0.o
 obj/sareadlinkat.o
+obj/sareadlinkat0.o
 # samisc.h
 obj/sacoloff.o
 obj/saoff2ptr.o
diff --git a/src/sareadlink0.c b/src/sareadlink0.c
new file mode 100644
index 0000000..61865c7
--- /dev/null
+++ b/src/sareadlink0.c
@@ -0,0 +1,15 @@
+#include <skalibs/djbunix.h>
+
+int
+sareadlink0(stralloc *sa, const char * restrict file)
+{
+    int r = sareadlink(sa, file);
+    if (r < 0)
+        return r;
+
+    /* make sure it ends with a NUL - which isn't guaranteed */
+    if (sa->s[sa->len - 1] != 0 && !stralloc_0(sa))
+        return -1;
+
+    return 0;
+}
diff --git a/src/sareadlinkat0.c b/src/sareadlinkat0.c
new file mode 100644
index 0000000..0c5adea
--- /dev/null
+++ b/src/sareadlinkat0.c
@@ -0,0 +1,15 @@
+#include "limb/djbunix.h"
+
+int
+sareadlinkat0(stralloc *sa, int fd, const char * restrict file)
+{
+    int r = sareadlinkat(sa, fd, file);
+    if (r < 0)
+        return r;
+
+    /* make sure it ends with a NUL - which isn't guaranteed */
+    if (sa->s[sa->len - 1] != 0 && !stralloc_0(sa))
+        return -1;
+
+    return 0;
+}