From 5b71542de3ef7f08b7c30e93340502d7cc120910 Mon Sep 17 00:00:00 2001 From: Dmitry Antipov Date: Wed, 14 Aug 2013 20:36:16 +0400 Subject: [PATCH] Utility function and macro to copy Lisp string to C string. * lisp.h (xlispstrdupa): New macro. (xlispstrdup): New prototype. * alloc.c (xlispstrdup): New function. * callint.c (Fcall_interactively): * fileio.c (Ffile_name_directory, Fexpand_file_name) (Fsubstitute_in_file_name): * frame.c (Fmake_terminal_frame): Use xlispstrdupa. * image.c (x_create_bitmap_from_file): * w32term.c (w32_term_init): * xterm.c (x_term_init): Use xlispstrdup. --- src/ChangeLog | 14 ++++++++++++++ src/alloc.c | 9 +++++++++ src/callint.c | 9 +++------ src/fileio.c | 9 +++------ src/frame.c | 12 ++---------- src/image.c | 6 ++---- src/lisp.h | 7 +++++++ src/w32term.c | 4 +--- src/xterm.c | 4 +--- 9 files changed, 42 insertions(+), 32 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 8a3186b85f2..415c4c3f525 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,17 @@ +2013-08-14 Dmitry Antipov + + Utility function and macro to copy Lisp string to C string. + * lisp.h (xlispstrdupa): New macro. + (xlispstrdup): New prototype. + * alloc.c (xlispstrdup): New function. + * callint.c (Fcall_interactively): + * fileio.c (Ffile_name_directory, Fexpand_file_name) + (Fsubstitute_in_file_name): + * frame.c (Fmake_terminal_frame): Use xlispstrdupa. + * image.c (x_create_bitmap_from_file): + * w32term.c (w32_term_init): + * xterm.c (x_term_init): Use xlispstrdup. + 2013-08-14 Lars Magne Ingebrigtsen * image.c (imagemagick_load_image): Make animated pictures work. diff --git a/src/alloc.c b/src/alloc.c index 2c2232601cb..c0d8c32b440 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -802,6 +802,15 @@ xstrdup (const char *s) return memcpy (xmalloc (size), s, size); } +/* Like above, but duplicates Lisp string to C string. */ + +char * +xlispstrdup (Lisp_Object string) +{ + ptrdiff_t size = SBYTES (string) + 1; + return memcpy (xmalloc (size), SSDATA (string), size); +} + /* Like putenv, but (1) use the equivalent of xmalloc and (2) the argument is a const pointer. */ diff --git a/src/callint.c b/src/callint.c index f43a5a990db..25096af5068 100644 --- a/src/callint.c +++ b/src/callint.c @@ -331,12 +331,9 @@ invoke it. If KEYS is omitted or nil, the return value of /* If SPECS is set to a string, use it as an interactive prompt. */ if (STRINGP (specs)) - { - /* Make a copy of string so that if a GC relocates specs, - `string' will still be valid. */ - string = alloca (SBYTES (specs) + 1); - memcpy (string, SSDATA (specs), SBYTES (specs) + 1); - } + /* Make a copy of string so that if a GC relocates specs, + `string' will still be valid. */ + string = xlispstrdupa (specs); else { Lisp_Object input; diff --git a/src/fileio.c b/src/fileio.c index 6ec5f78c2cf..08caf102266 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -366,8 +366,7 @@ Given a Unix syntax file name, returns a string ending in slash. */) } #ifdef DOS_NT - beg = alloca (SBYTES (filename) + 1); - memcpy (beg, SSDATA (filename), SBYTES (filename) + 1); + beg = xlispstrdupa (filename); #else beg = SSDATA (filename); #endif @@ -944,8 +943,7 @@ filesystem tree, not (expand-file-name ".." dirname). */) #endif /* Make a local copy of nm[] to protect it from GC in DECODE_FILE below. */ - nm = alloca (SBYTES (name) + 1); - memcpy (nm, SSDATA (name), SBYTES (name) + 1); + nm = xlispstrdupa (name); #ifdef DOS_NT /* Note if special escape prefix is present, but remove for now. */ @@ -1693,8 +1691,7 @@ those `/' is discarded. */) /* Always work on a copy of the string, in case GC happens during decode of environment variables, causing the original Lisp_String data to be relocated. */ - nm = alloca (SBYTES (filename) + 1); - memcpy (nm, SDATA (filename), SBYTES (filename) + 1); + nm = xlispstrdupa (filename); #ifdef DOS_NT dostounix_filename (nm, multibyte); diff --git a/src/frame.c b/src/frame.c index a1f151a221e..bb44f3cc987 100644 --- a/src/frame.c +++ b/src/frame.c @@ -692,22 +692,14 @@ affects all frames on the same terminal device. */) ? FRAME_TTY (XFRAME (selected_frame))->name : NULL)); if (!NILP (tty)) - { - name = alloca (SBYTES (tty) + 1); - memcpy (name, SSDATA (tty), SBYTES (tty)); - name[SBYTES (tty)] = 0; - } + name = xlispstrdupa (tty); tty_type = get_future_frame_param (Qtty_type, parms, (FRAME_TERMCAP_P (XFRAME (selected_frame)) ? FRAME_TTY (XFRAME (selected_frame))->type : NULL)); if (!NILP (tty_type)) - { - type = alloca (SBYTES (tty_type) + 1); - memcpy (type, SSDATA (tty_type), SBYTES (tty_type)); - type[SBYTES (tty_type)] = 0; - } + type = xlispstrdupa (tty_type); t = init_tty (name, type, 0); /* Errors are not fatal. */ } diff --git a/src/image.c b/src/image.c index b2d6726c1a4..f71ba211d44 100644 --- a/src/image.c +++ b/src/image.c @@ -302,11 +302,10 @@ x_create_bitmap_from_file (struct frame *f, Lisp_Object file) id = x_allocate_bitmap_record (f); dpyinfo->bitmaps[id - 1].img = bitmap; dpyinfo->bitmaps[id - 1].refcount = 1; - dpyinfo->bitmaps[id - 1].file = xmalloc (SBYTES (file) + 1); + dpyinfo->bitmaps[id - 1].file = xlispstrdup (file); dpyinfo->bitmaps[id - 1].depth = 1; dpyinfo->bitmaps[id - 1].height = ns_image_width (bitmap); dpyinfo->bitmaps[id - 1].width = ns_image_height (bitmap); - strcpy (dpyinfo->bitmaps[id - 1].file, SSDATA (file)); return id; #endif @@ -345,11 +344,10 @@ x_create_bitmap_from_file (struct frame *f, Lisp_Object file) dpyinfo->bitmaps[id - 1].pixmap = bitmap; dpyinfo->bitmaps[id - 1].have_mask = 0; dpyinfo->bitmaps[id - 1].refcount = 1; - dpyinfo->bitmaps[id - 1].file = xmalloc (SBYTES (file) + 1); + dpyinfo->bitmaps[id - 1].file = xlispstrdup (file); dpyinfo->bitmaps[id - 1].depth = 1; dpyinfo->bitmaps[id - 1].height = height; dpyinfo->bitmaps[id - 1].width = width; - strcpy (dpyinfo->bitmaps[id - 1].file, SSDATA (file)); return id; #endif /* HAVE_X_WINDOWS */ diff --git a/src/lisp.h b/src/lisp.h index 8dca073a83a..6d79bb1d6a5 100644 --- a/src/lisp.h +++ b/src/lisp.h @@ -4255,10 +4255,17 @@ extern void *xnrealloc (void *, ptrdiff_t, ptrdiff_t); extern void *xpalloc (void *, ptrdiff_t *, ptrdiff_t, ptrdiff_t, ptrdiff_t); extern char *xstrdup (const char *); +extern char *xlispstrdup (Lisp_Object); extern void xputenv (const char *); extern char *egetenv (const char *); +/* Copy Lisp string to temporary (allocated on stack) C string. */ + +#define xlispstrdupa(string) \ + memcpy (alloca (SBYTES (string) + 1), \ + SSDATA (string), SBYTES (string) + 1) + /* Set up the name of the machine we're running on. */ extern void init_system_name (void); diff --git a/src/w32term.c b/src/w32term.c index 03a9af12ea8..7d51850559b 100644 --- a/src/w32term.c +++ b/src/w32term.c @@ -6463,9 +6463,7 @@ w32_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name) terminal = w32_create_terminal (dpyinfo); /* Set the name of the terminal. */ - terminal->name = xmalloc (SBYTES (display_name) + 1); - strncpy (terminal->name, SDATA (display_name), SBYTES (display_name)); - terminal->name[SBYTES (display_name)] = 0; + terminal->name = xlispstrdup (display_name); dpyinfo->xrdb = xrm_option ? w32_make_rdb (xrm_option) : NULL; diff --git a/src/xterm.c b/src/xterm.c index 15ad3bdf851..9e1e32a2faf 100644 --- a/src/xterm.c +++ b/src/xterm.c @@ -10038,9 +10038,7 @@ x_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name) dpyinfo->display = dpy; /* Set the name of the terminal. */ - terminal->name = xmalloc (SBYTES (display_name) + 1); - memcpy (terminal->name, SSDATA (display_name), SBYTES (display_name)); - terminal->name[SBYTES (display_name)] = 0; + terminal->name = xlispstrdup (display_name); #if 0 XSetAfterFunction (x_current_display, x_trace_wire); -- 2.39.2