aboutsummaryrefslogtreecommitdiffstats
path: root/lib-src
diff options
context:
space:
mode:
authorPaul Eggert2018-11-21 14:47:53 -0800
committerPaul Eggert2018-11-21 14:48:59 -0800
commit0f22bf099e569790fe6bb830522b5e41be41bbb6 (patch)
treea72cb78be6398386d18dc7f8954e8fd17e874d45 /lib-src
parentcdb0d080f1bb2239ccb828d989d6bb73409d5f59 (diff)
downloademacs-0f22bf099e569790fe6bb830522b5e41be41bbb6.tar.gz
emacs-0f22bf099e569790fe6bb830522b5e41be41bbb6.zip
emacsclient: omit EXTRA_SPACE guesswork
* lib-src/emacsclient.c: Include <intprops.h>. (EXTRA_SPACE): Remove; code no longer guesses this is enough. (open_config): New function. (get_server_config): Use it. (set_local_socket): Compute upper bound of buffer size instead of guessing via EXTRA_SPACE.
Diffstat (limited to 'lib-src')
-rw-r--r--lib-src/emacsclient.c62
1 files changed, 31 insertions, 31 deletions
diff --git a/lib-src/emacsclient.c b/lib-src/emacsclient.c
index b7d4e813764..908602ec254 100644
--- a/lib-src/emacsclient.c
+++ b/lib-src/emacsclient.c
@@ -84,6 +84,7 @@ char *w32_getenv (const char *);
84#include <unistd.h> 84#include <unistd.h>
85 85
86#include <dosname.h> 86#include <dosname.h>
87#include <intprops.h>
87#include <min-max.h> 88#include <min-max.h>
88#include <unlocked-io.h> 89#include <unlocked-io.h>
89 90
@@ -91,8 +92,6 @@ char *w32_getenv (const char *);
91#define VERSION "unspecified" 92#define VERSION "unspecified"
92#endif 93#endif
93 94
94/* Additional space when allocating buffers for filenames, etc. */
95#define EXTRA_SPACE 100
96 95
97/* Name used to invoke this program. */ 96/* Name used to invoke this program. */
98static char const *progname; 97static char const *progname;
@@ -903,6 +902,26 @@ initialize_sockets (void)
903# endif /* WINDOWSNT */ 902# endif /* WINDOWSNT */
904 903
905 904
905/* If the home directory is HOME, return the configuration file with
906 basename CONFIG_FILE. Fail if there is no home directory or if the
907 configuration file could not be opened. */
908
909static FILE *
910open_config (char const *home, char const *config_file)
911{
912 if (!home)
913 return NULL;
914 ptrdiff_t homelen = strlen (home);
915 static char const emacs_d_server[] = "/.emacs.d/server/";
916 ptrdiff_t suffixsize = sizeof emacs_d_server + strlen (config_file);
917 char *configname = xmalloc (homelen + suffixsize);
918 strcpy (stpcpy (stpcpy (configname, home), emacs_d_server), config_file);
919
920 FILE *config = fopen (configname, "rb");
921 free (configname);
922 return config;
923}
924
906/* Read the information needed to set up a TCP comm channel with 925/* Read the information needed to set up a TCP comm channel with
907 the Emacs server: host, port, and authentication string. */ 926 the Emacs server: host, port, and authentication string. */
908 927
@@ -912,35 +931,16 @@ get_server_config (const char *config_file, struct sockaddr_in *server,
912{ 931{
913 char dotted[32]; 932 char dotted[32];
914 char *port; 933 char *port;
915 FILE *config = NULL; 934 FILE *config;
916 935
917 if (IS_ABSOLUTE_FILE_NAME (config_file)) 936 if (IS_ABSOLUTE_FILE_NAME (config_file))
918 config = fopen (config_file, "rb"); 937 config = fopen (config_file, "rb");
919 else 938 else
920 { 939 {
921 const char *home = egetenv ("HOME"); 940 config = open_config (egetenv ("HOME"), config_file);
922
923 if (home)
924 {
925 char *path = xmalloc (strlen (home) + strlen (config_file)
926 + EXTRA_SPACE);
927 char *z = stpcpy (path, home);
928 z = stpcpy (z, "/.emacs.d/server/");
929 strcpy (z, config_file);
930 config = fopen (path, "rb");
931 free (path);
932 }
933# ifdef WINDOWSNT 941# ifdef WINDOWSNT
934 if (!config && (home = egetenv ("APPDATA"))) 942 if (!config)
935 { 943 config = open_config (egetenv ("APPDATA"), config_file);
936 char *path = xmalloc (strlen (home) + strlen (config_file)
937 + EXTRA_SPACE);
938 char *z = stpcpy (path, home);
939 z = stpcpy (z, "/.emacs.d/server/");
940 strcpy (z, config_file);
941 config = fopen (path, "rb");
942 free (path);
943 }
944# endif 944# endif
945 } 945 }
946 946
@@ -1203,6 +1203,8 @@ set_local_socket (const char *local_socket_name)
1203 char *tmpdir_storage = NULL; 1203 char *tmpdir_storage = NULL;
1204 char *socket_name_storage = NULL; 1204 char *socket_name_storage = NULL;
1205 static char const subdir_format[] = "/emacs%"PRIuMAX"/"; 1205 static char const subdir_format[] = "/emacs%"PRIuMAX"/";
1206 int subdir_size_bound = (sizeof subdir_format - sizeof "%"PRIuMAX
1207 + INT_STRLEN_BOUND (uid_t) + 1);
1206 1208
1207 if (! (strchr (local_socket_name, '/') 1209 if (! (strchr (local_socket_name, '/')
1208 || (ISSLASH ('\\') && strchr (local_socket_name, '\\')))) 1210 || (ISSLASH ('\\') && strchr (local_socket_name, '\\'))))
@@ -1227,10 +1229,9 @@ set_local_socket (const char *local_socket_name)
1227 tmpdir = "/tmp"; 1229 tmpdir = "/tmp";
1228 } 1230 }
1229 socket_name_storage = 1231 socket_name_storage =
1230 xmalloc (strlen (tmpdir) + strlen (server_name) + EXTRA_SPACE); 1232 xmalloc (strlen (tmpdir) + strlen (server_name) + subdir_size_bound);
1231 char *z = stpcpy (socket_name_storage, tmpdir); 1233 char *z = stpcpy (socket_name_storage, tmpdir);
1232 z += sprintf (z, subdir_format, uid); 1234 strcpy (z + sprintf (z, subdir_format, uid), server_name);
1233 strcpy (z, server_name);
1234 local_socket_name = socket_name_storage; 1235 local_socket_name = socket_name_storage;
1235 } 1236 }
1236 1237
@@ -1268,10 +1269,9 @@ set_local_socket (const char *local_socket_name)
1268 uintmax_t uid = pw->pw_uid; 1269 uintmax_t uid = pw->pw_uid;
1269 char *user_socket_name 1270 char *user_socket_name
1270 = xmalloc (strlen (tmpdir) + strlen (server_name) 1271 = xmalloc (strlen (tmpdir) + strlen (server_name)
1271 + EXTRA_SPACE); 1272 + subdir_size_bound);
1272 char *z = stpcpy (user_socket_name, tmpdir); 1273 char *z = stpcpy (user_socket_name, tmpdir);
1273 z += sprintf (z, subdir_format, uid); 1274 strcpy (z + sprintf (z, subdir_format, uid), server_name);
1274 strcpy (z, server_name);
1275 1275
1276 if (strlen (user_socket_name) < sizeof (server.sun_path)) 1276 if (strlen (user_socket_name) < sizeof (server.sun_path))
1277 strcpy (server.sun_path, user_socket_name); 1277 strcpy (server.sun_path, user_socket_name);