From 66d12f660d33de130dc2f90b59c55e7407a86ffc Mon Sep 17 00:00:00 2001
From: "Rafael G. Martins" <rafael@rafaelmartins.eng.br>
Date: Fri, 18 Nov 2016 01:00:29 +0100
Subject: git-receiver: avoid static buffer.

this simplifies code, but makes it less portable
---
 src/blogc-git-receiver/pre-receive.c | 32 ++++++++++++++------------------
 1 file changed, 14 insertions(+), 18 deletions(-)

(limited to 'src')

diff --git a/src/blogc-git-receiver/pre-receive.c b/src/blogc-git-receiver/pre-receive.c
index 954c379..14d5ca2 100644
--- a/src/blogc-git-receiver/pre-receive.c
+++ b/src/blogc-git-receiver/pre-receive.c
@@ -93,7 +93,6 @@ int
 bgr_pre_receive_hook(int argc, char *argv[])
 {
     int rv = 0;
-    char buffer[4096];
     char *master = NULL;
 
     if (isatty(STDIN_FILENO)) {
@@ -148,15 +147,14 @@ bgr_pre_receive_hook(int argc, char *argv[])
     char *repo_dir = NULL;
     char *output_dir = NULL;
 
-    if (NULL == getcwd(buffer, sizeof(buffer))) {
-        fprintf(stderr, "error: failed to get repository remote path: %s\n",
+    repo_dir = getcwd(NULL, 0);
+    if (repo_dir == NULL) {
+        fprintf(stderr, "error: failed to get repository path: %s\n",
             strerror(errno));
         rv = 1;
         goto cleanup;
     }
 
-    repo_dir = bc_strdup(buffer);
-
     char dir[] = "/tmp/blogc_XXXXXX";
     if (NULL == mkdtemp(dir)) {
         rv = 1;
@@ -239,18 +237,13 @@ bgr_pre_receive_hook(int argc, char *argv[])
         goto cleanup;
     }
 
-    char *htdocs_sym = NULL;
-    ssize_t htdocs_sym_len = readlink("htdocs", buffer, sizeof(buffer));
-    if (0 < htdocs_sym_len) {
-        if (0 != unlink("htdocs")) {
-            fprintf(stderr, "error: failed to remove symlink (%s/htdocs): %s\n",
-                repo_dir, strerror(errno));
-            rmdir_recursive(output_dir);
-            rv = 1;
-            goto cleanup;
-        }
-        buffer[htdocs_sym_len] = '\0';
-        htdocs_sym = buffer;
+    char *htdocs_sym = realpath("htdocs", NULL);
+    if ((htdocs_sym != NULL) && (0 != unlink("htdocs"))) {
+        fprintf(stderr, "error: failed to remove symlink (%s/htdocs): %s\n",
+            repo_dir, strerror(errno));
+        rmdir_recursive(output_dir);
+        rv = 1;
+        goto cleanup2;
     }
 
     if (0 != symlink(output_dir, "htdocs")) {
@@ -258,12 +251,15 @@ bgr_pre_receive_hook(int argc, char *argv[])
             repo_dir, strerror(errno));
         rmdir_recursive(output_dir);
         rv = 1;
-        goto cleanup;
+        goto cleanup2;
     }
 
     if (htdocs_sym != NULL)
         rmdir_recursive(htdocs_sym);
 
+cleanup2:
+    free(htdocs_sym);
+
 cleanup:
     free(master);
     free(output_dir);
-- 
cgit v1.2.3-18-g5258