aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEli Zaretskii1998-11-11 11:12:09 +0000
committerEli Zaretskii1998-11-11 11:12:09 +0000
commit4b2e75e6adacb911456ffe94514abc8f57d8b857 (patch)
treecdbce619f84b3fe114e4f716ac7c9066aaab5f3e /src
parentea12aa2bf9838750162d7bf69534f1e274bab103 (diff)
downloademacs-4b2e75e6adacb911456ffe94514abc8f57d8b857.tar.gz
emacs-4b2e75e6adacb911456ffe94514abc8f57d8b857.zip
(MAX_ALLOCA): New macro.
(Fbase64_encode_region, Fbase64_encode_string, Fbase64_decode_region, Fbase64_decode_string): Don't allocate more than MAX_ALLOCA bytes with alloca; otherwise use xmalloc.
Diffstat (limited to 'src')
-rw-r--r--src/fns.c42
1 files changed, 36 insertions, 6 deletions
diff --git a/src/fns.c b/src/fns.c
index 0c24f7a4e3f..c31f8237dff 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -2752,6 +2752,10 @@ ARGS are passed as extra arguments to the function.")
2752#define IS_BASE64(Character) \ 2752#define IS_BASE64(Character) \
2753 (IS_ASCII (Character) && base64_char_to_value[Character] >= 0) 2753 (IS_ASCII (Character) && base64_char_to_value[Character] >= 0)
2754 2754
2755/* Don't use alloca for regions larger than this, lest we overflow
2756 their stack. */
2757#define MAX_ALLOCA 16*1024
2758
2755/* Table of characters coding the 64 values. */ 2759/* Table of characters coding the 64 values. */
2756static char base64_value_to_char[64] = 2760static char base64_value_to_char[64] =
2757{ 2761{
@@ -2831,7 +2835,10 @@ into shorter lines.")
2831 allength = length + length/3 + 1; 2835 allength = length + length/3 + 1;
2832 allength += allength / MIME_LINE_LENGTH + 1 + 6; 2836 allength += allength / MIME_LINE_LENGTH + 1 + 6;
2833 2837
2834 encoded = (char *) alloca (allength); 2838 if (allength <= MAX_ALLOCA)
2839 encoded = (char *) alloca (allength);
2840 else
2841 encoded = (char *) xmalloc (allength);
2835 encoded_length = base64_encode_1 (BYTE_POS_ADDR (ibeg), encoded, length, 2842 encoded_length = base64_encode_1 (BYTE_POS_ADDR (ibeg), encoded, length,
2836 NILP (no_line_break)); 2843 NILP (no_line_break));
2837 if (encoded_length > allength) 2844 if (encoded_length > allength)
@@ -2841,6 +2848,8 @@ into shorter lines.")
2841 and delete the old. (Insert first in order to preserve markers.) */ 2848 and delete the old. (Insert first in order to preserve markers.) */
2842 SET_PT_BOTH (XFASTINT (beg), ibeg); 2849 SET_PT_BOTH (XFASTINT (beg), ibeg);
2843 insert (encoded, encoded_length); 2850 insert (encoded, encoded_length);
2851 if (allength > MAX_ALLOCA)
2852 free (encoded);
2844 del_range_byte (ibeg + encoded_length, iend + encoded_length, 1); 2853 del_range_byte (ibeg + encoded_length, iend + encoded_length, 1);
2845 2854
2846 /* If point was outside of the region, restore it exactly; else just 2855 /* If point was outside of the region, restore it exactly; else just
@@ -2863,6 +2872,7 @@ DEFUN ("base64-encode-string", Fbase64_encode_string, Sbase64_encode_string,
2863{ 2872{
2864 int allength, length, encoded_length; 2873 int allength, length, encoded_length;
2865 char *encoded; 2874 char *encoded;
2875 Lisp_Object encoded_string;
2866 2876
2867 CHECK_STRING (string, 1); 2877 CHECK_STRING (string, 1);
2868 2878
@@ -2870,14 +2880,21 @@ DEFUN ("base64-encode-string", Fbase64_encode_string, Sbase64_encode_string,
2870 allength = length + length/3 + 1 + 6; 2880 allength = length + length/3 + 1 + 6;
2871 2881
2872 /* We need to allocate enough room for decoding the text. */ 2882 /* We need to allocate enough room for decoding the text. */
2873 encoded = (char *) alloca (allength); 2883 if (allength <= MAX_ALLOCA)
2884 encoded = (char *) alloca (allength);
2885 else
2886 encoded = (char *) xmalloc (allength);
2874 2887
2875 encoded_length = base64_encode_1 (XSTRING (string)->data, 2888 encoded_length = base64_encode_1 (XSTRING (string)->data,
2876 encoded, length, 0); 2889 encoded, length, 0);
2877 if (encoded_length > allength) 2890 if (encoded_length > allength)
2878 abort (); 2891 abort ();
2879 2892
2880 return make_unibyte_string (encoded, encoded_length); 2893 encoded_string = make_unibyte_string (encoded, encoded_length);
2894 if (allength > MAX_ALLOCA)
2895 free (encoded);
2896
2897 return encoded_string;
2881} 2898}
2882 2899
2883static int 2900static int
@@ -2975,7 +2992,10 @@ If the region can't be decoded, return nil and don't modify the buffer.")
2975 2992
2976 length = iend - ibeg; 2993 length = iend - ibeg;
2977 /* We need to allocate enough room for decoding the text. */ 2994 /* We need to allocate enough room for decoding the text. */
2978 decoded = (char *) alloca (length); 2995 if (length <= MAX_ALLOCA)
2996 decoded = (char *) alloca (length);
2997 else
2998 decoded = (char *) xmalloc (length);
2979 2999
2980 move_gap_both (XFASTINT (beg), ibeg); 3000 move_gap_both (XFASTINT (beg), ibeg);
2981 decoded_length = base64_decode_1 (BYTE_POS_ADDR (ibeg), decoded, length); 3001 decoded_length = base64_decode_1 (BYTE_POS_ADDR (ibeg), decoded, length);
@@ -2996,6 +3016,8 @@ If the region can't be decoded, return nil and don't modify the buffer.")
2996 TEMP_SET_PT_BOTH (XFASTINT (beg) + 1, ibeg + 1); 3016 TEMP_SET_PT_BOTH (XFASTINT (beg) + 1, ibeg + 1);
2997 insert (decoded, decoded_length); 3017 insert (decoded, decoded_length);
2998 inserted_chars = PT - (XFASTINT (beg) + 1); 3018 inserted_chars = PT - (XFASTINT (beg) + 1);
3019 if (length > MAX_ALLOCA)
3020 free (decoded);
2999 /* At first delete the original text. This never cause byte 3021 /* At first delete the original text. This never cause byte
3000 combining. */ 3022 combining. */
3001 del_range_both (PT + 1, PT_BYTE + 1, XFASTINT (end) + inserted_chars + 2, 3023 del_range_both (PT + 1, PT_BYTE + 1, XFASTINT (end) + inserted_chars + 2,
@@ -3025,12 +3047,16 @@ DEFUN ("base64-decode-string", Fbase64_decode_string, Sbase64_decode_string,
3025{ 3047{
3026 char *decoded; 3048 char *decoded;
3027 int length, decoded_length; 3049 int length, decoded_length;
3050 Lisp_Object decoded_string;
3028 3051
3029 CHECK_STRING (string, 1); 3052 CHECK_STRING (string, 1);
3030 3053
3031 length = STRING_BYTES (XSTRING (string)); 3054 length = STRING_BYTES (XSTRING (string));
3032 /* We need to allocate enough room for decoding the text. */ 3055 /* We need to allocate enough room for decoding the text. */
3033 decoded = (char *) alloca (length); 3056 if (length <= MAX_ALLOCA)
3057 decoded = (char *) alloca (length);
3058 else
3059 decoded = (char *) xmalloc (length);
3034 3060
3035 decoded_length = base64_decode_1 (XSTRING (string)->data, decoded, length); 3061 decoded_length = base64_decode_1 (XSTRING (string)->data, decoded, length);
3036 if (decoded_length > length) 3062 if (decoded_length > length)
@@ -3039,7 +3065,11 @@ DEFUN ("base64-decode-string", Fbase64_decode_string, Sbase64_decode_string,
3039 if (decoded_length < 0) 3065 if (decoded_length < 0)
3040 return Qnil; 3066 return Qnil;
3041 3067
3042 return make_string (decoded, decoded_length); 3068 decoded_string = make_string (decoded, decoded_length);
3069 if (length > MAX_ALLOCA)
3070 free (decoded);
3071
3072 return decoded_string;
3043} 3073}
3044 3074
3045static int 3075static int