From da477db1ea5cedf6c41782368e7537ae8ce3173d Mon Sep 17 00:00:00 2001 From: jjgarcia Date: Mon, 18 Nov 2002 11:26:33 +0000 Subject: [PATCH] Introduce function coerce_to_simple_string() to ensure that C functions get a null-terminated string --- src/c/file.d | 3 ++- src/c/string.d | 9 ++++++++- src/c/tcp.d | 9 ++++++--- src/c/unixfsys.d | 8 +++++--- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/c/file.d b/src/c/file.d index c337e77ef..c0102fb60 100644 --- a/src/c/file.d +++ b/src/c/file.d @@ -251,7 +251,8 @@ open_stream(cl_object fn, enum smmode smm, cl_object if_exists, { cl_object x; FILE *fp; - char *fname = coerce_to_filename(fn)->string.self; + cl_object filename = coerce_to_filename(fn); + char *fname = filename->string.self; if (smm == smm_input || smm == smm_probe) { fp = fopen(fname, OPEN_R); diff --git a/src/c/string.d b/src/c/string.d index d176e2084..7b366c1e7 100644 --- a/src/c/string.d +++ b/src/c/string.d @@ -116,6 +116,13 @@ copy_simple_string(cl_object x) return(y); } +cl_object +coerce_to_simple_string(cl_object x) +{ + assert_type_string(x); + return x->string.adjustable? copy_simple_string(x) : x; +} + cl_object cl_string(cl_object x) { @@ -220,7 +227,7 @@ string_eq(cl_object x, cl_object y) cl_index i, j; i = x->string.fillp; j = y->string.fillp; - return (i == j && strncmp(x->string.self, y->string.self, i) == 0); + return (i == j && memcmp(x->string.self, y->string.self, i) == 0); } diff --git a/src/c/tcp.d b/src/c/tcp.d index a68d5b612..36e0c7931 100644 --- a/src/c/tcp.d +++ b/src/c/tcp.d @@ -239,8 +239,11 @@ si_open_client_stream(cl_object host, cl_object port) int fd, p; /* file descriptor */ cl_object streamIn, streamOut; - assert_type_string(host); /* Ensure "host" is a string */ - p = fixnnint(port); /* INV: fixnnint() checks type */ + /* Ensure "host" is a string that we can pass to a C function */ + host = coerce_to_simple_string(host); + + /* The port number is not negative */ + p = fixnnint(port); if (host->string.fillp > BUFSIZ - 1) FEerror("~S is a too long file name.", 1, host); @@ -334,7 +337,7 @@ si_lookup_host_entry(cl_object host_or_address) switch (type_of(host_or_address)) { case t_string: - host_or_address->string.self[host_or_address->string.fillp] = 0; + host_or_address = coerce_to_simple_string(host_or_address); he = gethostbyname(host_or_address->string.self); break; case t_fixnum: diff --git a/src/c/unixfsys.d b/src/c/unixfsys.d index 0cdfb922e..a7eb5a790 100644 --- a/src/c/unixfsys.d +++ b/src/c/unixfsys.d @@ -168,6 +168,7 @@ truedirectory(cl_object pathname) if (chdir("..") < 0) return error_no_dir(pathname); } else if (type_of(name) == t_string) { + name = coerce_to_simple_string(name); if (chdir(name->string.self) < 0) return error_no_dir(pathname); } else @@ -280,7 +281,6 @@ cl_delete_file(cl_object file) { cl_object filename; - /* INV: coerce_to_filename() checks types */ filename = coerce_to_filename(file); if (unlink(filename->string.self) < 0) FEfilesystem_error("Cannot delete the file ~S.", 1, file); @@ -307,7 +307,6 @@ cl_file_write_date(cl_object file) cl_object filename, time; struct stat filestatus; - /* INV: coerce_to_filename() checks types */ filename = coerce_to_filename(file); if (stat(filename->string.self, &filestatus) < 0) time = Cnil; @@ -326,7 +325,6 @@ cl_file_author(cl_object file) extern struct passwd *getpwuid(uid_t); #endif - /* INV: coerce_to_filename() checks types */ filename = coerce_to_filename(file); if (stat(filename->string.self, &filestatus) < 0) FEfilesystem_error("Cannot get the file status of ~S.", 1, @@ -492,6 +490,8 @@ actual_directory(cl_object namestring, cl_object mask, bool all) DIR *dir; struct dirent *entry; + namestring = coerce_to_simple_string(namestring); + mask = coerce_to_simple_string(mask); if (chdir(namestring->string.self) < 0) { chdir(saved_dir->string.self); FEfilesystem_error("directory: cannot access ~A", 1, namestring); @@ -521,6 +521,8 @@ actual_directory(cl_object namestring, cl_object mask, bool all) char iobuffer[BUFSIZ]; DIRECTORY dir; + namestring = coerce_to_simple_string(namestring); + mask = coerce_to_simple_string(mask); if (chdir(namestring->string.self) < 0) { chdir(saved_dir->string.self); FEfilesystem_error("directory: cannot access ~A",1,namestring);