]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
man: Don't leak memory in path-documents example
authorThomas Mühlbacher <tmuehlbacher@posteo.net>
Mon, 30 Aug 2021 14:16:30 +0000 (16:16 +0200)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 31 Aug 2021 04:44:49 +0000 (13:44 +0900)
The `sd_path_lookup(3)` man page states that the returned string shall be
`free(3)`'d but then doesn't do so in the example code.

Also add basic error handling as well.

man/path-documents.c

index a6c1f9371ae22d9318963cec2eb6e04fbadc1fa1..082d6c29fb4636ad697bf4b193e14deb37928459 100644 (file)
@@ -1,9 +1,17 @@
 #include <stdio.h>
+#include <stdlib.h>
 #include <sd-path.h>
 
 int main(void) {
+  int r;
   char *t;
 
-  sd_path_lookup(SD_PATH_USER_DOCUMENTS, NULL, &t);
+  r = sd_path_lookup(SD_PATH_USER_DOCUMENTS, NULL, &t);
+  if (r < 0)
+    return EXIT_FAILURE;
+
   printf("~/Documents: %s\n", t);
+  free(t);
+
+  return EXIT_SUCCESS;
 }