From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: tangxiaofeng <tangxiaofeng@kylinos.cn>
Date: Mon, 19 Oct 2026 22:42:17 +0000
Subject: [PATCH] Fix 'c0' negative-size check in struct.unpack on arm64/riscv64

Description: reject a negative 'c0' size before converting it to size_t.
 Converting a negative lua_Number to size_t is undefined behaviour; on
 arm64 and riscv64 the conversion saturates to 0, so the "data string too
 short" check was silently bypassed and struct.unpack('bc0', '\255')
 returned an empty string instead of raising an error.  This made the
 upstream test teststruct.lua:234 fail (the same failure occurs on arm64
 and on riscv64).  The explicit non-negative check raises the very same
 error with the same message as before on architectures where the
 conversion previously yielded a huge value (amd64, loong64), so their
 behaviour is unchanged.
Origin: vendor
Forwarded: not-needed
Author: tangxiaofeng <tangxiaofeng@kylinos.cn>
--- lua-struct-0.3.orig/struct.c
+++ lua-struct-0.3/struct.c
@@ -330,10 +330,17 @@ static int b_unpack (lua_State *L) {
       }
       case 'c': {
         if (size == 0) {
+          lua_Number sz;
           if (n == 0 || !lua_isnumber(L, -1))
             luaL_error(L, "format 'c0' needs a previous size");
-          size = lua_tonumber(L, -1);
+          sz = lua_tonumber(L, -1);
           lua_pop(L, 1); n--;
+          /* reject a negative size before converting it to size_t: that
+             conversion is undefined for negative values and saturates to 0
+             on some architectures (e.g. arm64, riscv64), which would
+             silently bypass the 'data string too short' check below */
+          luaL_argcheck(L, sz >= 0, 2, "data string too short");
+          size = (size_t)sz;
           luaL_argcheck(L, size <= ld - pos, 2, "data string too short");
         }
         lua_pushlstring(L, data+pos, size); n++;
