@simonklee/opentui-tex-native-source 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/native/lib.zig CHANGED
@@ -114,6 +114,9 @@ fn render(
114
114
  foreground: [3]u8,
115
115
  background: [3]u8,
116
116
  ) !void {
117
+ if (!std.unicode.utf8ValidateSlice(source) or std.mem.indexOfScalar(u8, source, 0) != null) {
118
+ return error.InvalidTex;
119
+ }
117
120
  var arena = std.heap.ArenaAllocator.init(std.heap.c_allocator);
118
121
  defer arena.deinit();
119
122
  const allocator = arena.allocator();
@@ -124,7 +127,7 @@ fn render(
124
127
  "#{x:0>2}{x:0>2}{x:0>2}",
125
128
  .{ foreground[0], foreground[1], foreground[2] },
126
129
  );
127
- const renderer = &(renderer_instance orelse return error.InternalError);
130
+ const renderer = if (renderer_instance) |*value| value else return error.InternalError;
128
131
  const svg = renderer.parseRender(allocator, source, .{
129
132
  .size = 28,
130
133
  .spacing = 8,
@@ -137,11 +140,15 @@ fn render(
137
140
  const mutable_svg = try svgWithoutDescription(allocator, svg);
138
141
  const image = c.nsvgParse(mutable_svg.ptr, "px", 96) orelse return error.InvalidSvg;
139
142
  defer c.nsvgDelete(image);
140
- const width: usize = @intFromFloat(@ceil(image.*.width * supersample));
141
- const height: usize = @intFromFloat(@ceil(image.*.height * supersample));
142
- if (width == 0 or height == 0 or width > dimension_max or height > dimension_max) {
143
+ const width_float = @ceil(image.*.width * supersample);
144
+ const height_float = @ceil(image.*.height * supersample);
145
+ if (!(width_float > 0 and width_float <= dimension_max) or
146
+ !(height_float > 0 and height_float <= dimension_max))
147
+ {
143
148
  return error.InvalidDimensions;
144
149
  }
150
+ const width: usize = @intFromFloat(width_float);
151
+ const height: usize = @intFromFloat(height_float);
145
152
  const pixel_length = std.math.mul(usize, width, height) catch return error.InvalidDimensions;
146
153
  const pixels_length = std.math.mul(usize, pixel_length, channels) catch return error.InvalidDimensions;
147
154
  if (pixels_length > std.math.maxInt(u32)) return error.InvalidDimensions;