aboutsummaryrefslogtreecommitdiffstats
path: root/lib-src
diff options
context:
space:
mode:
authorPaul Eggert2018-11-21 10:50:38 -0800
committerPaul Eggert2018-11-21 10:53:30 -0800
commit7a85a40ef402460eafe3254df4f916369829ea21 (patch)
tree42c5dcf43f089b8bf55abde224e7a1150a2deaea /lib-src
parente01d030723aef2f90a2fc53a0b5251f29df94527 (diff)
downloademacs-7a85a40ef402460eafe3254df4f916369829ea21.tar.gz
emacs-7a85a40ef402460eafe3254df4f916369829ea21.zip
emacsclient: fix unlikely crash with "&"
* lib-src/emacsclient.c (quote_argument): Mention *DATA in comment so it’s clear DATA must be non-null. (quote_argument, unquote_argument): Simplify. (unquote_argument): Don’t crash if the string ends in "&".
Diffstat (limited to 'lib-src')
-rw-r--r--lib-src/emacsclient.c85
1 files changed, 34 insertions, 51 deletions
diff --git a/lib-src/emacsclient.c b/lib-src/emacsclient.c
index 66ada43908e..e3e1d9b16d3 100644
--- a/lib-src/emacsclient.c
+++ b/lib-src/emacsclient.c
@@ -772,10 +772,10 @@ sock_err_message (const char *function_name)
772} 772}
773 773
774 774
775/* Let's send the data to Emacs when either 775/* Send to S the data in *DATA when either
776 - the data ends in "\n", or 776 - the data's last byte is '\n', or
777 - the buffer is full (but this shouldn't happen) 777 - the buffer is full (but this shouldn't happen)
778 Otherwise, we just accumulate it. */ 778 Otherwise, just accumulate the data. */
779static void 779static void
780send_to_emacs (HSOCKET s, const char *data) 780send_to_emacs (HSOCKET s, const char *data)
781{ 781{
@@ -823,33 +823,21 @@ static void
823quote_argument (HSOCKET s, const char *str) 823quote_argument (HSOCKET s, const char *str)
824{ 824{
825 char *copy = xmalloc (strlen (str) * 2 + 1); 825 char *copy = xmalloc (strlen (str) * 2 + 1);
826 const char *p; 826 char *q = copy;
827 char *q; 827 if (*str == '-')
828 828 *q++ = '&', *q++ = *str++;
829 p = str; 829 for (; *str; str++)
830 q = copy;
831 while (*p)
832 { 830 {
833 if (*p == ' ') 831 char c = *str;
834 { 832 if (c == ' ')
835 *q++ = '&'; 833 *q++ = '&', c = '_';
836 *q++ = '_'; 834 else if (c == '\n')
837 p++; 835 *q++ = '&', c = 'n';
838 } 836 else if (c == '&')
839 else if (*p == '\n') 837 *q++ = '&';
840 { 838 *q++ = c;
841 *q++ = '&';
842 *q++ = 'n';
843 p++;
844 }
845 else
846 {
847 if (*p == '&' || (*p == '-' && p == str))
848 *q++ = '&';
849 *q++ = *p++;
850 }
851 } 839 }
852 *q++ = 0; 840 *q = 0;
853 841
854 send_to_emacs (s, copy); 842 send_to_emacs (s, copy);
855 843
@@ -857,36 +845,31 @@ quote_argument (HSOCKET s, const char *str)
857} 845}
858 846
859 847
860/* The inverse of quote_argument. Removes quoting in string STR by 848/* The inverse of quote_argument. Remove quoting in string STR by
861 modifying the string in place. Returns STR. */ 849 modifying the addressed string in place. Return STR. */
862 850
863static char * 851static char *
864unquote_argument (char *str) 852unquote_argument (char *str)
865{ 853{
866 char *p, *q; 854 char const *p = str;
867 855 char *q = str;
868 if (! str) 856 char c;
869 return str;
870 857
871 p = str; 858 do
872 q = str;
873 while (*p)
874 { 859 {
875 if (*p == '&') 860 c = *p++;
876 { 861 if (c == '&')
877 p++; 862 {
878 if (*p == '&') 863 c = *p++;
879 *p = '&'; 864 if (c == '_')
880 else if (*p == '_') 865 c = ' ';
881 *p = ' '; 866 else if (c == 'n')
882 else if (*p == 'n') 867 c = '\n';
883 *p = '\n'; 868 }
884 else if (*p == '-') 869 *q++ = c;
885 *p = '-';
886 }
887 *q++ = *p++;
888 } 870 }
889 *q = 0; 871 while (c);
872
890 return str; 873 return str;
891} 874}
892 875