emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

scratch/comp-static-data 0246e122df8 04/16: Avoid accessing Lisp_Vector'


From: Vibhav Pant
Subject: scratch/comp-static-data 0246e122df8 04/16: Avoid accessing Lisp_Vector's size field directly.
Date: Thu, 19 Jan 2023 12:44:33 -0500 (EST)

branch: scratch/comp-static-data
commit 0246e122df8a3107df57d17226be198656f89071
Author: Vibhav Pant <vibhavp@gmail.com>
Commit: Vibhav Pant <vibhavp@gmail.com>

    Avoid accessing Lisp_Vector's size field directly.
    
    When Emacs is built with support for statically emitted Lisp literals
    during native compilation, the generated objects (in this case,
    vectors) have their mark bits set to 1. This may cause code that
    accesses header.size directly (i.e, not through ASIZE) to get invalid
    values as the vector's length, causing further problems down the
    line.
    
    * src/lisp.h (VECTOR_ASIZE): New function.
    * src/ccl.c (setup_ccl_program): Use VECTOR_ASIZE to get vp's length.
    * src/indent.c (disptab_matches_widthtab, recompute_width_table): Use
    VECTOR_ASIZE to get widthtab's length.
    * src/process.c (Fformat_network_address): Use vector macros in lisp.h
    to access address's fields and length.
    (conv_addrinfo_to_lisp, get_lisp_to_sockaddr_size): Use ASIZE to get
    address's length.
    * src/window.c (Fset_window_configuration): Use VECTOR_ASIZE to get
    saved_window's length.
    (compare_window_configurations): Use VECTOR_ASIZE to get sws1's
    length.
    * src/xdisp.c (setup_for_ellipsis, get_next_display_element,
    on_hot_spot_p): Use VECTOR_ASIZE to get v's length.
---
 src/ccl.c     |  2 +-
 src/indent.c  |  4 ++--
 src/lisp.h    | 10 ++++++++++
 src/process.c | 23 ++++++++++-------------
 src/window.c  |  8 ++++----
 src/xdisp.c   |  8 ++++----
 6 files changed, 31 insertions(+), 24 deletions(-)

diff --git a/src/ccl.c b/src/ccl.c
index 1a4f73500a3..8f53832f62b 100644
--- a/src/ccl.c
+++ b/src/ccl.c
@@ -2001,7 +2001,7 @@ setup_ccl_program (struct ccl_program *ccl, Lisp_Object 
ccl_prog)
       if (! VECTORP (ccl_prog))
        return false;
       vp = XVECTOR (ccl_prog);
-      ccl->size = vp->header.size;
+      ccl->size = VECTOR_ASIZE (vp);
       ccl->prog = vp->contents;
       ccl->eof_ic = XFIXNUM (vp->contents[CCL_HEADER_EOF]);
       ccl->buf_magnification = XFIXNUM (vp->contents[CCL_HEADER_BUF_MAG]);
diff --git a/src/indent.c b/src/indent.c
index 4671ccccf90..a444fcc2091 100644
--- a/src/indent.c
+++ b/src/indent.c
@@ -111,7 +111,7 @@ disptab_matches_widthtab (struct Lisp_Char_Table *disptab, 
struct Lisp_Vector *w
 {
   int i;
 
-  eassert (widthtab->header.size == 256);
+  eassert (VECTOR_ASIZE (widthtab) == 256);
 
   for (i = 0; i < 256; i++)
     if (character_width (i, disptab)
@@ -132,7 +132,7 @@ recompute_width_table (struct buffer *buf, struct 
Lisp_Char_Table *disptab)
   if (!VECTORP (BVAR (buf, width_table)))
     bset_width_table (buf, make_uninit_vector (256));
   widthtab = XVECTOR (BVAR (buf, width_table));
-  eassert (widthtab->header.size == 256);
+  eassert (VECTOR_ASIZE (widthtab) == 256);
 
   for (i = 0; i < 256; i++)
     XSETFASTINT (widthtab->contents[i], character_width (i, disptab));
diff --git a/src/lisp.h b/src/lisp.h
index 3791bf2b0c3..2ceffd47345 100644
--- a/src/lisp.h
+++ b/src/lisp.h
@@ -1810,6 +1810,16 @@ ASIZE (Lisp_Object array)
   return size;
 }
 
+INLINE ptrdiff_t
+VECTOR_ASIZE (struct Lisp_Vector *v)
+{
+#ifdef HAVE_STATIC_LISP_GLOBALS
+  return v->header.size & ~ARRAY_MARK_FLAG;
+#else
+  return v->header.size;
+#endif
+}
+
 INLINE ptrdiff_t
 PVSIZE (Lisp_Object pv)
 {
diff --git a/src/process.c b/src/process.c
index 5144c5d6c92..03bd272f683 100644
--- a/src/process.c
+++ b/src/process.c
@@ -1666,8 +1666,7 @@ Return nil if format of ADDRESS is invalid.  */)
 
   if (VECTORP (address))  /* AF_INET or AF_INET6 */
     {
-      register struct Lisp_Vector *p = XVECTOR (address);
-      ptrdiff_t size = p->header.size;
+      ptrdiff_t size = ASIZE (address);
       Lisp_Object args[10];
       int nargs, i;
       char const *format;
@@ -1700,15 +1699,15 @@ Return nil if format of ADDRESS is invalid.  */)
 
       for (i = 0; i < nargs; i++)
        {
-         if (! RANGED_FIXNUMP (0, p->contents[i], 65535))
+         if (! RANGED_FIXNUMP (0, AREF (address, i), 65535))
            return Qnil;
 
          if (nargs <= 5         /* IPv4 */
              && i < 4           /* host, not port */
-             && XFIXNUM (p->contents[i]) > 255)
+             && XFIXNUM (AREF (address, i)) > 255)
            return Qnil;
 
-         args[i + 1] = p->contents[i];
+         args[i + 1] = AREF (address, i);
        }
 
       return Fformat (nargs + 1, args);
@@ -2634,18 +2633,16 @@ conv_addrinfo_to_lisp (struct addrinfo *res)
 static ptrdiff_t
 get_lisp_to_sockaddr_size (Lisp_Object address, int *familyp)
 {
-  struct Lisp_Vector *p;
-
   if (VECTORP (address))
     {
-      p = XVECTOR (address);
-      if (p->header.size == 5)
+      ptrdiff_t size = ASIZE (address);
+      if (size == 5)
        {
          *familyp = AF_INET;
          return sizeof (struct sockaddr_in);
        }
 #ifdef AF_INET6
-      else if (p->header.size == 9)
+      else if (size == 9)
        {
          *familyp = AF_INET6;
          return sizeof (struct sockaddr_in6);
@@ -2663,11 +2660,11 @@ get_lisp_to_sockaddr_size (Lisp_Object address, int 
*familyp)
           && VECTORP (XCDR (address)))
     {
       struct sockaddr *sa;
-      p = XVECTOR (XCDR (address));
-      if (MAX_ALLOCA - sizeof sa->sa_family < p->header.size)
+      Lisp_Object p = XCDR (address);
+      if (MAX_ALLOCA - sizeof sa->sa_family < ASIZE (p))
        return 0;
       *familyp = XFIXNUM (XCAR (address));
-      return p->header.size + sizeof (sa->sa_family);
+      return ASIZE (p) + sizeof (sa->sa_family);
     }
   return 0;
 }
diff --git a/src/window.c b/src/window.c
index 90fa6ac2dfe..2793d73e3cc 100644
--- a/src/window.c
+++ b/src/window.c
@@ -7114,7 +7114,7 @@ the return value is nil.  Otherwise the value is t.  */)
 
       /* Don't do this within the main loop below: This may call Lisp
         code and is thus potentially unsafe while input is blocked.  */
-      for (k = 0; k < saved_windows->header.size; k++)
+      for (k = 0; k < VECTOR_ASIZE (saved_windows); k++)
        {
          p = SAVED_WINDOW_N (saved_windows, k);
          window = p->window;
@@ -7169,7 +7169,7 @@ the return value is nil.  Otherwise the value is t.  */)
         dead.  */
       delete_all_child_windows (FRAME_ROOT_WINDOW (f));
 
-      for (k = 0; k < saved_windows->header.size; k++)
+      for (k = 0; k < VECTOR_ASIZE (saved_windows); k++)
        {
          p = SAVED_WINDOW_N (saved_windows, k);
          window = p->window;
@@ -8233,10 +8233,10 @@ compare_window_configurations (Lisp_Object 
configuration1,
       || !EQ (d1->f_current_buffer, d2->f_current_buffer)
       || !EQ (d1->focus_frame, d2->focus_frame)
       /* Verify that the two configurations have the same number of windows.  
*/
-      || sws1->header.size != sws2->header.size)
+      || VECTOR_ASIZE (sws1) != VECTOR_ASIZE (sws2))
     return false;
 
-  for (i = 0; i < sws1->header.size; i++)
+  for (i = 0; i < VECTOR_ASIZE (sws1); i++)
     {
       struct saved_window *sw1, *sw2;
 
diff --git a/src/xdisp.c b/src/xdisp.c
index e8df230ef89..795697bca76 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -5217,7 +5217,7 @@ setup_for_ellipsis (struct it *it, int len)
     {
       struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
       it->dpvec = v->contents;
-      it->dpend = v->contents + v->header.size;
+      it->dpend = v->contents + VECTOR_ASIZE(v);
     }
   else
     {
@@ -7953,11 +7953,11 @@ get_next_display_element (struct it *it)
              /* Return the first character from the display table
                 entry, if not empty.  If empty, don't display the
                 current character.  */
-             if (v->header.size)
+             if (VECTOR_ASIZE (v))
                {
                  it->dpvec_char_len = it->len;
                  it->dpvec = v->contents;
-                 it->dpend = v->contents + v->header.size;
+                 it->dpend = v->contents + VECTOR_ASIZE (v);
                  it->current.dpvec_index = 0;
                  it->dpvec_face_id = -1;
                  it->saved_face_id = it->face_id;
@@ -34439,7 +34439,7 @@ on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
        {
          struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
          Lisp_Object *poly = v->contents;
-         ptrdiff_t n = v->header.size;
+         ptrdiff_t n = VECTOR_ASIZE (v);
          ptrdiff_t i;
          bool inside = false;
          Lisp_Object lx, ly;



reply via email to

[Prev in Thread] Current Thread [Next in Thread]