From 20d47ae8db2b40c81e3f307f7d0478935946b417 Mon Sep 17 00:00:00 2001
From: Jessica Clarke <jrtc27@jrtc27.com>
Date: Fri, 12 Aug 2022 19:27:08 +0100
Subject: [PATCH 1/8] tclCompile: Be more careful about alignment in
 TclInitByteCode

The current code rounds up various sizes to ensure offsets remain
aligned, but adds them to sizeof(ByteCode) without rounding that up. In
practice this doesn't matter as ByteCode ends up being aligned, and thus
padded, enough for everything after, but add the extra TCL_ALIGN to
avoid problems creeping in in future.
---
 generic/tclCompile.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/generic/tclCompile.c b/generic/tclCompile.c
index 5bfad3780..a57743c7f 100644
--- a/generic/tclCompile.c
+++ b/generic/tclCompile.c
@@ -2825,9 +2825,13 @@ TclInitByteCode(
 
     /*
      * Compute the total number of bytes needed for this bytecode.
+     *
+     * Note that code bytes need not be aligned but since later elements are we
+     * need to pad anyway, either directly after ByteCode or after codeBytes,
+     * and it's easier and more consistent to do the former.
      */
 
-    structureSize = sizeof(ByteCode);
+    structureSize = TCL_ALIGN(sizeof(ByteCode));  /* align code bytes */
     structureSize += TCL_ALIGN(codeBytes);	  /* align object array */
     structureSize += TCL_ALIGN(objArrayBytes);	  /* align exc range arr */
     structureSize += TCL_ALIGN(exceptArrayBytes); /* align AuxData array */
@@ -2866,7 +2870,7 @@ TclInitByteCode(
     codePtr->maxExceptDepth = envPtr->maxExceptDepth;
     codePtr->maxStackDepth = envPtr->maxStackDepth;
 
-    p += sizeof(ByteCode);
+    p += TCL_ALIGN(sizeof(ByteCode));	/* align code bytes */
     codePtr->codeStart = p;
     memcpy(p, envPtr->codeStart, codeBytes);
 
-- 
2.34.GIT

