Fix strVal to handle pointer-to-array types correctly
- For pointer-to-array (*[N]u8), explicitly slice using @ptrCast - Prevents returning dangling pointer to stack memory
This commit is contained in:
@@ -198,12 +198,26 @@ fn generateSingleFile(
|
|||||||
\\
|
\\
|
||||||
\\fn strVal(v: anytype) []const u8 {
|
\\fn strVal(v: anytype) []const u8 {
|
||||||
\\ const T = @TypeOf(v);
|
\\ const T = @TypeOf(v);
|
||||||
\\ return switch (@typeInfo(T)) {
|
\\ switch (@typeInfo(T)) {
|
||||||
\\ .pointer => |p| if (p.size == .slice) v else @compileError("unsupported pointer type"),
|
\\ .pointer => |p| switch (p.size) {
|
||||||
\\ .int, .comptime_int => std.fmt.bufPrint(&int_buf, "{d}", .{v}) catch "0",
|
\\ .slice => return v,
|
||||||
\\ .optional => if (v) |val| strVal(val) else "",
|
\\ .one => {
|
||||||
|
\\ // For pointer-to-array, slice it
|
||||||
|
\\ const child_info = @typeInfo(p.child);
|
||||||
|
\\ if (child_info == .array) {
|
||||||
|
\\ const arr_info = child_info.array;
|
||||||
|
\\ const ptr: [*]const arr_info.child = @ptrCast(v);
|
||||||
|
\\ return ptr[0..arr_info.len];
|
||||||
|
\\ }
|
||||||
|
\\ return strVal(v.*);
|
||||||
|
\\ },
|
||||||
|
\\ else => @compileError("unsupported pointer type"),
|
||||||
|
\\ },
|
||||||
|
\\ .array => @compileError("arrays must be passed by pointer"),
|
||||||
|
\\ .int, .comptime_int => return std.fmt.bufPrint(&int_buf, "{d}", .{v}) catch "0",
|
||||||
|
\\ .optional => return if (v) |val| strVal(val) else "",
|
||||||
\\ else => @compileError("strVal: unsupported type " ++ @typeName(T)),
|
\\ else => @compileError("strVal: unsupported type " ++ @typeName(T)),
|
||||||
\\ };
|
\\ }
|
||||||
\\}
|
\\}
|
||||||
\\
|
\\
|
||||||
\\// ─────────────────────────────────────────────────────────────────────────────
|
\\// ─────────────────────────────────────────────────────────────────────────────
|
||||||
|
|||||||
Reference in New Issue
Block a user