Hi,

Attached are two patchs to unhardcode the maximum buffer size used as
argument to getcwd.  Please select at most one (if any).

The first one
(0001-Use-C_MAX_PATH-as-maximum-buffer-size-for-getcwd.patch) is just a
simple replacement (it replaces 1024 with C_MAX_PATH).

I tend to think that it would be better to have the maximum buffer size
given as argument to C_curdir, so that we don't risk having a mismatch
between the actual buffer size given to C_curdir and the buffer size
given to getcwd.  Assuming C_curdir is not part of the C API, the second
patch (0001-Make-C_curdir-get-the-buffer-size-as-argument.patch)
uses that approach.

All the best.
Mario
-- 
http://parenteses.org/mario
>From edff979befa47a03a7d2686fab498c9e77008a08 Mon Sep 17 00:00:00 2001
From: Mario Domenech Goulart <[email protected]>
Date: Mon, 22 Aug 2022 22:20:04 +0200
Subject: [PATCH] Use C_MAX_PATH as maximum buffer size for getcwd

Basically replace the hardcoded value (1024) with the value of
C_MAX_PATH for the maximum buffer size for getcwd.
---
 library.scm | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/library.scm b/library.scm
index a8103a47..6726bed3 100644
--- a/library.scm
+++ b/library.scm
@@ -5883,7 +5883,7 @@ EOF
 #>
 
 #define C_chdir(str) C_fix(chdir(C_c_string(str)))
-#define C_curdir(buf) (getcwd(C_c_string(buf), 1024) ? C_fix(strlen(C_c_string(buf))) : C_SCHEME_FALSE)
+#define C_curdir(buf) (getcwd(C_c_string(buf), C_MAX_PATH) ? C_fix(strlen(C_c_string(buf))) : C_SCHEME_FALSE)
 #define C_getenventry(i) (environ[ i ])
 
 #ifdef HAVE_CRT_EXTERNS_H
@@ -5949,7 +5949,7 @@ static C_word C_fcall C_setenv(C_word x, C_word y) {
 (define current-directory
   (getter-with-setter
    (lambda ()
-     (let* ((buffer (make-string 1024))
+     (let* ((buffer (make-string (foreign-value "C_MAX_PATH" size_t)))
 	    (len (##core#inline "C_curdir" buffer)))
       (unless ##sys#windows-platform ; FIXME need `cond-expand' here
 	(##sys#update-errno))
-- 
2.30.2

>From 84ad1f64aa90fe61ce3b794468c17f6986d1965e Mon Sep 17 00:00:00 2001
From: Mario Domenech Goulart <[email protected]>
Date: Mon, 22 Aug 2022 22:20:04 +0200
Subject: [PATCH] Make C_curdir get the buffer size as argument

Make C_curdir get the buffer size as argument, so that it can be used
in the call to getcwd.

Also replace the hardcoded value (1024) with the value of C_MAX_PATH
for the maximum buffer size in the call to C_curdir.
---
 library.scm | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/library.scm b/library.scm
index a8103a47..9fc663e0 100644
--- a/library.scm
+++ b/library.scm
@@ -5883,7 +5883,7 @@ EOF
 #>
 
 #define C_chdir(str) C_fix(chdir(C_c_string(str)))
-#define C_curdir(buf) (getcwd(C_c_string(buf), 1024) ? C_fix(strlen(C_c_string(buf))) : C_SCHEME_FALSE)
+#define C_curdir(buf, size) (getcwd(C_c_string(buf), size) ? C_fix(strlen(C_c_string(buf))) : C_SCHEME_FALSE)
 #define C_getenventry(i) (environ[ i ])
 
 #ifdef HAVE_CRT_EXTERNS_H
@@ -5949,8 +5949,9 @@ static C_word C_fcall C_setenv(C_word x, C_word y) {
 (define current-directory
   (getter-with-setter
    (lambda ()
-     (let* ((buffer (make-string 1024))
-	    (len (##core#inline "C_curdir" buffer)))
+     (let* ((buffer-size (foreign-value "C_MAX_PATH" size_t))
+            (buffer (make-string buffer-size))
+            (len (##core#inline "C_curdir" buffer buffer-size)))
       (unless ##sys#windows-platform ; FIXME need `cond-expand' here
 	(##sys#update-errno))
       (if len
-- 
2.30.2

Reply via email to