bare-image-resample 1.0.2 → 1.0.4

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
@@ -1,12 +1,22 @@
1
1
  #include <assert.h>
2
2
  #include <bare.h>
3
3
  #include <js.h>
4
+ #include <stdint.h>
4
5
 
5
6
  #define STB_IMAGE_RESIZE_IMPLEMENTATION
6
7
  #define STB_IMAGE_RESIZE_STATIC
7
8
 
8
9
  #include <stb_image_resize2.h>
9
10
 
11
+ // A pixel is four bytes, one 8-bit channel each for R, G, B and A. It is the
12
+ // only layout this binding handles: it is what STBIR_RGBA asks stbir_resize()
13
+ // for, and it is how both the source and the target buffers are laid out.
14
+ static const int64_t BYTES_PER_PIXEL = 4;
15
+
16
+ // Cap the resampled pixel count to keep the requested dimensions from reaching
17
+ // the allocator. 256 Mpx at 4 bytes per pixel covers up to a 16384x16384 image.
18
+ #define BARE_IMAGE_RESAMPLE_MAX_PIXELS (1ull << 28)
19
+
10
20
  static js_value_t *
11
21
  bare_image_resample_resize(js_env_t *env, js_callback_info_t *info) {
12
22
  int err;
@@ -20,7 +30,8 @@ bare_image_resample_resize(js_env_t *env, js_callback_info_t *info) {
20
30
  assert(argc == 5);
21
31
 
22
32
  uint8_t *source;
23
- err = js_get_typedarray_info(env, argv[0], NULL, (void **) &source, NULL, NULL, NULL);
33
+ size_t source_len;
34
+ err = js_get_typedarray_info(env, argv[0], NULL, (void **) &source, &source_len, NULL, NULL);
24
35
  assert(err == 0);
25
36
 
26
37
  int64_t source_width;
@@ -39,11 +50,58 @@ bare_image_resample_resize(js_env_t *env, js_callback_info_t *info) {
39
50
  err = js_get_value_int64(env, argv[4], &target_height);
40
51
  assert(err == 0);
41
52
 
53
+ if (
54
+ source_width <= 0 || source_width > INT32_MAX ||
55
+ source_height <= 0 || source_height > INT32_MAX
56
+ ) {
57
+ err = js_throw_error(env, NULL, "Invalid source dimensions");
58
+ assert(err == 0);
59
+
60
+ return NULL;
61
+ }
62
+
63
+ if ((uint64_t) source_width * (uint64_t) source_height * BYTES_PER_PIXEL > (uint64_t) source_len) {
64
+ err = js_throw_error(env, NULL, "Source buffer too small for its dimensions");
65
+ assert(err == 0);
66
+
67
+ return NULL;
68
+ }
69
+
70
+ if (
71
+ target_width <= 0 || target_width > INT32_MAX ||
72
+ target_height <= 0 || target_height > INT32_MAX
73
+ ) {
74
+ err = js_throw_error(env, NULL, "Invalid target dimensions");
75
+ assert(err == 0);
76
+
77
+ return NULL;
78
+ }
79
+
80
+ if (
81
+ (uint64_t) source_width * (uint64_t) source_height > BARE_IMAGE_RESAMPLE_MAX_PIXELS ||
82
+ (uint64_t) target_width * (uint64_t) target_height > BARE_IMAGE_RESAMPLE_MAX_PIXELS
83
+ ) {
84
+ err = js_throw_error(env, NULL, "Image dimensions exceed maximum");
85
+ assert(err == 0);
86
+
87
+ return NULL;
88
+ }
89
+
90
+ uint64_t target_len = (uint64_t) target_width * (uint64_t) target_height * BYTES_PER_PIXEL;
91
+
92
+ if (target_len > SIZE_MAX) {
93
+ err = js_throw_error(env, NULL, "Target image too large");
94
+ assert(err == 0);
95
+
96
+ return NULL;
97
+ }
98
+
42
99
  js_value_t *result;
43
100
 
44
101
  uint8_t *target;
45
- err = js_create_unsafe_arraybuffer(env, target_width * target_height * 4, (void **) &target, &result);
46
- assert(err == 0);
102
+ err = js_create_unsafe_arraybuffer(env, (size_t) target_len, (void **) &target, &result);
103
+
104
+ if (err < 0) return NULL;
47
105
 
48
106
  stbir_resize(
49
107
  source,
package/index.js CHANGED
@@ -18,13 +18,7 @@ exports.resize = function resize(image, width = 0, height = 0, opts = {}) {
18
18
  width = Math.round(width)
19
19
  height = Math.round(height)
20
20
 
21
- const buffer = binding.resize(
22
- image.data,
23
- image.width,
24
- image.height,
25
- width,
26
- height
27
- )
21
+ const buffer = binding.resize(image.data, image.width, image.height, width, height)
28
22
 
29
23
  return {
30
24
  data: Buffer.from(buffer),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-image-resample",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Image resampling support for Bare",
5
5
  "exports": {
6
6
  "./package": "./package.json",
@@ -15,7 +15,9 @@
15
15
  ],
16
16
  "addon": true,
17
17
  "scripts": {
18
- "test": "prettier . --check && bare test.js"
18
+ "format": "prettier --write . && lunte --fix",
19
+ "lint": "prettier --check . && lunte",
20
+ "test": "brittle-bare --coverage test.js"
19
21
  },
20
22
  "repository": {
21
23
  "type": "git",
@@ -32,7 +34,8 @@
32
34
  "brittle": "^3.4.0",
33
35
  "cmake-bare": "^1.1.2",
34
36
  "cmake-fetch": "^1.4.6",
37
+ "lunte": "^1.8.0",
35
38
  "prettier": "^3.3.3",
36
- "prettier-config-standard": "^7.0.0"
39
+ "prettier-config-holepunch": "^2.0.0"
37
40
  }
38
41
  }