bare-ico 1.0.0 → 1.0.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/binding.c CHANGED
@@ -12,9 +12,9 @@
12
12
 
13
13
  // ICO file format structures
14
14
  typedef struct __attribute__((packed)) {
15
- uint16_t reserved; // Must be 0
16
- uint16_t type; // 1 for ICO, 2 for CUR
17
- uint16_t count; // Number of images
15
+ uint16_t reserved; // Must be 0
16
+ uint16_t type; // 1 for ICO, 2 for CUR
17
+ uint16_t count; // Number of images
18
18
  } IconDir;
19
19
 
20
20
  typedef struct __attribute__((packed)) {
@@ -133,7 +133,8 @@ bare_ico_decode(js_env_t *env, js_callback_info_t *info) {
133
133
  uint32_t offset = entry->image_offset;
134
134
  uint32_t size = entry->bytes_in_res;
135
135
 
136
- if (offset + size > ico_len) {
136
+ // Check bounds without overflowing uint32_t
137
+ if (offset > ico_len || size > (uint32_t) (ico_len - offset)) {
137
138
  err = js_throw_error(env, NULL, "Invalid ICO file: image data out of bounds");
138
139
  assert(err == 0);
139
140
  return NULL;
@@ -142,8 +143,7 @@ bare_ico_decode(js_env_t *env, js_callback_info_t *info) {
142
143
  uint8_t *img_data = ico_data + offset;
143
144
 
144
145
  // Check if PNG (magic: 0x89 0x50 0x4E 0x47)
145
- bool is_png = (size >= 8 && img_data[0] == 0x89 && img_data[1] == 0x50 &&
146
- img_data[2] == 0x4E && img_data[3] == 0x47);
146
+ bool is_png = (size >= 8 && img_data[0] == 0x89 && img_data[1] == 0x50 && img_data[2] == 0x4E && img_data[3] == 0x47);
147
147
 
148
148
  int width, height, channels;
149
149
  uint8_t *rgba_data = NULL;
@@ -177,8 +177,10 @@ bare_ico_decode(js_env_t *env, js_callback_info_t *info) {
177
177
  memcpy(&bmp_bpp, img_data + 14, 2);
178
178
  memcpy(&bmp_compression, img_data + 16, 4);
179
179
 
180
- // Validate
181
- if (header_size < 40 || bmp_width <= 0 || bmp_height <= 0) {
180
+ // Validate. Cap dimensions to keep all downstream size math within int32.
181
+ // 65535 is well above any realistic ICO image and prevents overflow in
182
+ // row_size / pixel_data_size / rgba_size calculations below.
183
+ if (header_size < 40 || bmp_width <= 0 || bmp_height <= 0 || bmp_width > 65535 || bmp_height > 131070) {
182
184
  err = js_throw_error(env, NULL, "Invalid BMP-in-ICO header");
183
185
  assert(err == 0);
184
186
  return NULL;
@@ -187,27 +189,37 @@ bare_ico_decode(js_env_t *env, js_callback_info_t *info) {
187
189
  // ICO BMP height is doubled (image data + AND mask)
188
190
  int32_t actual_height = bmp_height / 2;
189
191
 
190
- // Calculate row size (must be multiple of 4)
191
- int row_size = ((bmp_width * bmp_bpp + 31) / 32) * 4;
192
- int pixel_data_size = row_size * actual_height;
192
+ // Compute sizes in 64-bit to detect any wrap before we trust them.
193
+ int64_t row_size_64 = (((int64_t) bmp_width * bmp_bpp + 31) / 32) * 4;
194
+ int64_t pixel_data_size_64 = row_size_64 * actual_height;
195
+ int64_t rgba_size_64 = (int64_t) bmp_width * actual_height * 4;
196
+
197
+ // Palette size (only present for <=8bpp)
198
+ int palette_size = (bmp_bpp <= 8) ? (1 << bmp_bpp) * 4 : 0;
199
+
200
+ // Verify palette + pixel data lie entirely within the input image.
201
+ if ((int64_t) header_size + palette_size > (int64_t) size || pixel_data_size_64 > (int64_t) size - header_size - palette_size) {
202
+ err = js_throw_error(env, NULL, "Invalid BMP-in-ICO: pixel data out of bounds");
203
+ assert(err == 0);
204
+
205
+ return NULL;
206
+ }
207
+
208
+ int row_size = (int) row_size_64;
193
209
 
194
210
  // Allocate output RGBA buffer
195
211
  width = bmp_width;
196
212
  height = actual_height;
197
- rgba_data = (uint8_t *)malloc(width * height * 4);
213
+ rgba_data = (uint8_t *) malloc((size_t) rgba_size_64);
198
214
  if (!rgba_data) {
199
215
  err = js_throw_error(env, NULL, "Memory allocation failed");
200
216
  assert(err == 0);
217
+
201
218
  return NULL;
202
219
  }
203
220
 
204
- // Find pixel data start (after header and color palette if present)
205
- uint8_t *pixel_start = img_data + header_size;
206
- if (bmp_bpp <= 8) {
207
- // Has color palette
208
- int palette_size = (1 << bmp_bpp) * 4;
209
- pixel_start += palette_size;
210
- }
221
+ // Pixel data starts after header and (optional) color palette
222
+ uint8_t *pixel_start = img_data + header_size + palette_size;
211
223
 
212
224
  // Decode based on bit depth
213
225
  if (bmp_bpp == 32 && bmp_compression == 0) {
@@ -231,7 +243,7 @@ bare_ico_decode(js_env_t *env, js_callback_info_t *info) {
231
243
  dst[x * 4 + 0] = src[x * 3 + 2]; // R
232
244
  dst[x * 4 + 1] = src[x * 3 + 1]; // G
233
245
  dst[x * 4 + 2] = src[x * 3 + 0]; // B
234
- dst[x * 4 + 3] = 255; // A
246
+ dst[x * 4 + 3] = 255; // A
235
247
  }
236
248
  }
237
249
  } else if (bmp_bpp == 8 && bmp_compression == 0) {
@@ -245,7 +257,7 @@ bare_ico_decode(js_env_t *env, js_callback_info_t *info) {
245
257
  dst[x * 4 + 0] = palette[index * 4 + 2]; // R
246
258
  dst[x * 4 + 1] = palette[index * 4 + 1]; // G
247
259
  dst[x * 4 + 2] = palette[index * 4 + 0]; // B
248
- dst[x * 4 + 3] = 255; // A
260
+ dst[x * 4 + 3] = 255; // A
249
261
  }
250
262
  }
251
263
  } else if (bmp_bpp == 4 && bmp_compression == 0) {
@@ -265,7 +277,7 @@ bare_ico_decode(js_env_t *env, js_callback_info_t *info) {
265
277
  dst[x * 4 + 0] = palette[index * 4 + 2]; // R
266
278
  dst[x * 4 + 1] = palette[index * 4 + 1]; // G
267
279
  dst[x * 4 + 2] = palette[index * 4 + 0]; // B
268
- dst[x * 4 + 3] = 255; // A (opaque)
280
+ dst[x * 4 + 3] = 255; // A (opaque)
269
281
  }
270
282
  }
271
283
  } else {
@@ -304,8 +316,15 @@ bare_ico_decode(js_env_t *env, js_callback_info_t *info) {
304
316
  err = js_set_named_property(env, result, "height", height_val);
305
317
  assert(err == 0);
306
318
 
307
- // Set data
308
- size_t data_len = width * height * 4;
319
+ // Set data. Use size_t arithmetic to avoid int overflow on the PNG path
320
+ // where width/height come from stb_image and are not pre-bounded here.
321
+ if (width <= 0 || height <= 0) {
322
+ stbi_image_free(rgba_data);
323
+ err = js_throw_error(env, NULL, "Invalid decoded image dimensions");
324
+ assert(err == 0);
325
+ return NULL;
326
+ }
327
+ size_t data_len = (size_t) width * (size_t) height * 4;
309
328
  uint8_t *result_data = malloc(data_len);
310
329
  if (!result_data) {
311
330
  stbi_image_free(rgba_data);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-ico",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "ICO support for Bare",
5
5
  "main": "index.js",
6
6
  "exports": {
@@ -16,7 +16,9 @@
16
16
  ],
17
17
  "addon": true,
18
18
  "scripts": {
19
- "test": "prettier . --check && bare test.js"
19
+ "format": "prettier --write . && lunte --fix",
20
+ "lint": "prettier --check . && lunte",
21
+ "test": "brittle-bare --coverage test.js"
20
22
  },
21
23
  "repository": {
22
24
  "type": "git",
@@ -32,6 +34,7 @@
32
34
  "brittle": "^3.4.0",
33
35
  "cmake-bare": "^1.1.2",
34
36
  "cmake-fetch": "^1.0.0",
37
+ "lunte": "^1.8.0",
35
38
  "prettier": "^3.3.3",
36
39
  "prettier-config-holepunch": "^2.0.0"
37
40
  }
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file