aboutsummaryrefslogtreecommitdiffstats
path: root/src/xselect.c
diff options
context:
space:
mode:
authorPaul Eggert2011-06-03 12:02:25 -0700
committerPaul Eggert2011-06-03 12:02:25 -0700
commit201f31ae3de0b747b47863b93d6f6a747c36c960 (patch)
tree800c2c66fc648660cb957df676ea59cc911ae0fa /src/xselect.c
parentbe14b9ab109c8deb5745dc47cbc471e97be06486 (diff)
downloademacs-201f31ae3de0b747b47863b93d6f6a747c36c960.tar.gz
emacs-201f31ae3de0b747b47863b93d6f6a747c36c960.zip
Check for overflow when converting integer to cons and back.
* charset.c (Fdefine_charset_internal, Fdecode_char): Use cons_to_unsigned to catch overflow. (Fencode_char): Use INTEGER_TO_CONS. * composite.h (LGLYPH_CODE): Use cons_to_unsigned. (LGLYPH_SET_CODE): Use INTEGER_TO_CONS. * data.c (long_to_cons, cons_to_long): Remove. (cons_to_unsigned, cons_to_signed): New functions. These signal an error for invalid or out-of-range values. * dired.c (Ffile_attributes): Use INTEGER_TO_CONS. * fileio.c (Fset_visited_file_modtime): Use CONS_TO_INTEGER. * font.c (Ffont_variation_glyphs): * fontset.c (Finternal_char_font): Use INTEGER_TO_CONS. * lisp.h (INTEGER_TO_CONS, CONS_TO_INTEGER): New macros. (cons_to_signed, cons_to_unsigned): New decls. (long_to_cons, cons_to_long): Remove decls. * undo.c (record_first_change): Use INTEGER_TO_CONS. (Fprimitive_undo): Use CONS_TO_INTEGER. * xfns.c (Fx_window_property): Likewise. * xselect.c (x_own_selection, selection_data_to_lisp_data): Use INTEGER_TO_CONS. (x_handle_selection_request, x_handle_selection_clear) (x_get_foreign_selection, Fx_disown_selection_internal) (Fx_get_atom_name, x_send_client_event): Use CONS_TO_INTEGER. (lisp_data_to_selection_data): Use cons_to_unsigned. (x_fill_property_data): Use cons_to_signed. Report values out of range.
Diffstat (limited to 'src/xselect.c')
-rw-r--r--src/xselect.c87
1 files changed, 35 insertions, 52 deletions
diff --git a/src/xselect.c b/src/xselect.c
index ca2b1812a61..7545a44b1bc 100644
--- a/src/xselect.c
+++ b/src/xselect.c
@@ -335,7 +335,7 @@ x_own_selection (Lisp_Object selection_name, Lisp_Object selection_value,
335 Lisp_Object prev_value; 335 Lisp_Object prev_value;
336 336
337 selection_data = list4 (selection_name, selection_value, 337 selection_data = list4 (selection_name, selection_value,
338 long_to_cons (timestamp), frame); 338 INTEGER_TO_CONS (timestamp), frame);
339 prev_value = LOCAL_SELECTION (selection_name, dpyinfo); 339 prev_value = LOCAL_SELECTION (selection_name, dpyinfo);
340 340
341 dpyinfo->terminal->Vselection_alist 341 dpyinfo->terminal->Vselection_alist
@@ -419,7 +419,7 @@ x_get_local_selection (Lisp_Object selection_symbol, Lisp_Object target_type,
419 || INTEGERP (check) 419 || INTEGERP (check)
420 || NILP (value)) 420 || NILP (value))
421 return value; 421 return value;
422 /* Check for a value that cons_to_long could handle. */ 422 /* Check for a value that CONS_TO_INTEGER could handle. */
423 else if (CONSP (check) 423 else if (CONSP (check)
424 && INTEGERP (XCAR (check)) 424 && INTEGERP (XCAR (check))
425 && (INTEGERP (XCDR (check)) 425 && (INTEGERP (XCDR (check))
@@ -782,8 +782,8 @@ x_handle_selection_request (struct input_event *event)
782 if (NILP (local_selection_data)) goto DONE; 782 if (NILP (local_selection_data)) goto DONE;
783 783
784 /* Decline requests issued prior to our acquiring the selection. */ 784 /* Decline requests issued prior to our acquiring the selection. */
785 local_selection_time 785 CONS_TO_INTEGER (XCAR (XCDR (XCDR (local_selection_data))),
786 = (Time) cons_to_long (XCAR (XCDR (XCDR (local_selection_data)))); 786 Time, local_selection_time);
787 if (SELECTION_EVENT_TIME (event) != CurrentTime 787 if (SELECTION_EVENT_TIME (event) != CurrentTime
788 && local_selection_time > SELECTION_EVENT_TIME (event)) 788 && local_selection_time > SELECTION_EVENT_TIME (event))
789 goto DONE; 789 goto DONE;
@@ -950,8 +950,8 @@ x_handle_selection_clear (struct input_event *event)
950 /* Well, we already believe that we don't own it, so that's just fine. */ 950 /* Well, we already believe that we don't own it, so that's just fine. */
951 if (NILP (local_selection_data)) return; 951 if (NILP (local_selection_data)) return;
952 952
953 local_selection_time = (Time) 953 CONS_TO_INTEGER (XCAR (XCDR (XCDR (local_selection_data))),
954 cons_to_long (XCAR (XCDR (XCDR (local_selection_data)))); 954 Time, local_selection_time);
955 955
956 /* We have reasserted the selection since this SelectionClear was 956 /* We have reasserted the selection since this SelectionClear was
957 generated, so we can disregard it. */ 957 generated, so we can disregard it. */
@@ -1213,16 +1213,7 @@ x_get_foreign_selection (Lisp_Object selection_symbol, Lisp_Object target_type,
1213 return Qnil; 1213 return Qnil;
1214 1214
1215 if (! NILP (time_stamp)) 1215 if (! NILP (time_stamp))
1216 { 1216 CONS_TO_INTEGER (time_stamp, Time, requestor_time);
1217 if (CONSP (time_stamp))
1218 requestor_time = (Time) cons_to_long (time_stamp);
1219 else if (INTEGERP (time_stamp))
1220 requestor_time = (Time) XUINT (time_stamp);
1221 else if (FLOATP (time_stamp))
1222 requestor_time = (Time) XFLOAT_DATA (time_stamp);
1223 else
1224 error ("TIME_STAMP must be cons or number");
1225 }
1226 1217
1227 BLOCK_INPUT; 1218 BLOCK_INPUT;
1228 1219
@@ -1652,7 +1643,7 @@ selection_data_to_lisp_data (Display *display, const unsigned char *data,
1652 convert it to a cons of integers, 16 bits in each half. 1643 convert it to a cons of integers, 16 bits in each half.
1653 */ 1644 */
1654 else if (format == 32 && size == sizeof (unsigned int)) 1645 else if (format == 32 && size == sizeof (unsigned int))
1655 return long_to_cons (((unsigned int *) data) [0]); 1646 return INTEGER_TO_CONS (((unsigned int *) data) [0]);
1656 else if (format == 16 && size == sizeof (unsigned short)) 1647 else if (format == 16 && size == sizeof (unsigned short))
1657 return make_number ((int) (((unsigned short *) data) [0])); 1648 return make_number ((int) (((unsigned short *) data) [0]));
1658 1649
@@ -1678,7 +1669,7 @@ selection_data_to_lisp_data (Display *display, const unsigned char *data,
1678 for (i = 0; i < size / 4; i++) 1669 for (i = 0; i < size / 4; i++)
1679 { 1670 {
1680 unsigned int j = ((unsigned int *) data) [i]; 1671 unsigned int j = ((unsigned int *) data) [i];
1681 Faset (v, make_number (i), long_to_cons (j)); 1672 Faset (v, make_number (i), INTEGER_TO_CONS (j));
1682 } 1673 }
1683 return v; 1674 return v;
1684 } 1675 }
@@ -1755,7 +1746,7 @@ lisp_data_to_selection_data (Display *display, Lisp_Object obj,
1755 *size_ret = 1; 1746 *size_ret = 1;
1756 *data_ret = (unsigned char *) xmalloc (sizeof (unsigned long) + 1); 1747 *data_ret = (unsigned char *) xmalloc (sizeof (unsigned long) + 1);
1757 (*data_ret) [sizeof (unsigned long)] = 0; 1748 (*data_ret) [sizeof (unsigned long)] = 0;
1758 (*(unsigned long **) data_ret) [0] = cons_to_long (obj); 1749 (*(unsigned long **) data_ret) [0] = cons_to_unsigned (obj, ULONG_MAX);
1759 if (NILP (type)) type = QINTEGER; 1750 if (NILP (type)) type = QINTEGER;
1760 } 1751 }
1761 else if (VECTORP (obj)) 1752 else if (VECTORP (obj))
@@ -1803,11 +1794,11 @@ lisp_data_to_selection_data (Display *display, Lisp_Object obj,
1803 *data_ret = (unsigned char *) xmalloc (*size_ret * data_size); 1794 *data_ret = (unsigned char *) xmalloc (*size_ret * data_size);
1804 for (i = 0; i < *size_ret; i++) 1795 for (i = 0; i < *size_ret; i++)
1805 if (*format_ret == 32) 1796 if (*format_ret == 32)
1806 (*((unsigned long **) data_ret)) [i] 1797 (*((unsigned long **) data_ret)) [i] =
1807 = cons_to_long (XVECTOR (obj)->contents [i]); 1798 cons_to_unsigned (XVECTOR (obj)->contents [i], ULONG_MAX);
1808 else 1799 else
1809 (*((unsigned short **) data_ret)) [i] 1800 (*((unsigned short **) data_ret)) [i] =
1810 = (unsigned short) cons_to_long (XVECTOR (obj)->contents [i]); 1801 cons_to_unsigned (XVECTOR (obj)->contents [i], USHRT_MAX);
1811 } 1802 }
1812 } 1803 }
1813 else 1804 else
@@ -2025,8 +2016,10 @@ frame's display, or the first available X display. */)
2025 selection_atom = symbol_to_x_atom (dpyinfo, selection); 2016 selection_atom = symbol_to_x_atom (dpyinfo, selection);
2026 2017
2027 BLOCK_INPUT; 2018 BLOCK_INPUT;
2028 timestamp = (NILP (time_object) ? last_event_timestamp 2019 if (NILP (time_object))
2029 : cons_to_long (time_object)); 2020 timestamp = last_event_timestamp;
2021 else
2022 CONS_TO_INTEGER (time_object, Time, timestamp);
2030 XSetSelectionOwner (dpyinfo->display, selection_atom, None, timestamp); 2023 XSetSelectionOwner (dpyinfo->display, selection_atom, None, timestamp);
2031 UNBLOCK_INPUT; 2024 UNBLOCK_INPUT;
2032 2025
@@ -2232,12 +2225,8 @@ x_fill_property_data (Display *dpy, Lisp_Object data, void *ret, int format)
2232 { 2225 {
2233 Lisp_Object o = XCAR (iter); 2226 Lisp_Object o = XCAR (iter);
2234 2227
2235 if (INTEGERP (o)) 2228 if (INTEGERP (o) || FLOATP (o) || CONSP (o))
2236 val = (long) XFASTINT (o); 2229 val = cons_to_signed (o, LONG_MIN, LONG_MAX);
2237 else if (FLOATP (o))
2238 val = (long) XFLOAT_DATA (o);
2239 else if (CONSP (o))
2240 val = (long) cons_to_long (o);
2241 else if (STRINGP (o)) 2230 else if (STRINGP (o))
2242 { 2231 {
2243 BLOCK_INPUT; 2232 BLOCK_INPUT;
@@ -2248,9 +2237,19 @@ x_fill_property_data (Display *dpy, Lisp_Object data, void *ret, int format)
2248 error ("Wrong type, must be string, number or cons"); 2237 error ("Wrong type, must be string, number or cons");
2249 2238
2250 if (format == 8) 2239 if (format == 8)
2251 *d08++ = (char) val; 2240 {
2241 if (CHAR_MIN <= val && val <= CHAR_MAX)
2242 *d08++ = val;
2243 else
2244 error ("Out of 'char' range");
2245 }
2252 else if (format == 16) 2246 else if (format == 16)
2253 *d16++ = (short) val; 2247 {
2248 if (SHRT_MIN <= val && val <= SHRT_MAX)
2249 *d16++ = val;
2250 else
2251 error ("Out of 'short' range");
2252 }
2254 else 2253 else
2255 *d32++ = val; 2254 *d32++ = val;
2256 } 2255 }
@@ -2334,14 +2333,7 @@ If the value is 0 or the atom is not known, return the empty string. */)
2334 Atom atom; 2333 Atom atom;
2335 int had_errors; 2334 int had_errors;
2336 2335
2337 if (INTEGERP (value)) 2336 CONS_TO_INTEGER (value, Atom, atom);
2338 atom = (Atom) XUINT (value);
2339 else if (FLOATP (value))
2340 atom = (Atom) XFLOAT_DATA (value);
2341 else if (CONSP (value))
2342 atom = (Atom) cons_to_long (value);
2343 else
2344 error ("Wrong type, value must be number or cons");
2345 2337
2346 BLOCK_INPUT; 2338 BLOCK_INPUT;
2347 x_catch_errors (dpy); 2339 x_catch_errors (dpy);
@@ -2531,17 +2523,8 @@ x_send_client_event (Lisp_Object display, Lisp_Object dest, Lisp_Object from, At
2531 else 2523 else
2532 error ("DEST as a string must be one of PointerWindow or InputFocus"); 2524 error ("DEST as a string must be one of PointerWindow or InputFocus");
2533 } 2525 }
2534 else if (INTEGERP (dest)) 2526 else if (INTEGERP (dest) || FLOATP (dest) || CONSP (dest))
2535 wdest = (Window) XFASTINT (dest); 2527 CONS_TO_INTEGER (dest, Window, wdest);
2536 else if (FLOATP (dest))
2537 wdest = (Window) XFLOAT_DATA (dest);
2538 else if (CONSP (dest))
2539 {
2540 if (! NUMBERP (XCAR (dest)) || ! NUMBERP (XCDR (dest)))
2541 error ("Both car and cdr for DEST must be numbers");
2542 else
2543 wdest = (Window) cons_to_long (dest);
2544 }
2545 else 2528 else
2546 error ("DEST must be a frame, nil, string, number or cons"); 2529 error ("DEST must be a frame, nil, string, number or cons");
2547 2530