bare-image-resample 1.0.2 → 1.0.3

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,18 @@
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
+
10
16
  static js_value_t *
11
17
  bare_image_resample_resize(js_env_t *env, js_callback_info_t *info) {
12
18
  int err;
@@ -20,7 +26,8 @@ bare_image_resample_resize(js_env_t *env, js_callback_info_t *info) {
20
26
  assert(argc == 5);
21
27
 
22
28
  uint8_t *source;
23
- err = js_get_typedarray_info(env, argv[0], NULL, (void **) &source, NULL, NULL, NULL);
29
+ size_t source_len;
30
+ err = js_get_typedarray_info(env, argv[0], NULL, (void **) &source, &source_len, NULL, NULL);
24
31
  assert(err == 0);
25
32
 
26
33
  int64_t source_width;
@@ -39,10 +46,46 @@ bare_image_resample_resize(js_env_t *env, js_callback_info_t *info) {
39
46
  err = js_get_value_int64(env, argv[4], &target_height);
40
47
  assert(err == 0);
41
48
 
49
+ if (
50
+ source_width <= 0 || source_width > INT32_MAX ||
51
+ source_height <= 0 || source_height > INT32_MAX
52
+ ) {
53
+ err = js_throw_error(env, NULL, "Invalid source dimensions");
54
+ assert(err == 0);
55
+
56
+ return NULL;
57
+ }
58
+
59
+ if ((uint64_t) source_width * (uint64_t) source_height * BYTES_PER_PIXEL > (uint64_t) source_len) {
60
+ err = js_throw_error(env, NULL, "Source buffer too small for its dimensions");
61
+ assert(err == 0);
62
+
63
+ return NULL;
64
+ }
65
+
66
+ if (
67
+ target_width <= 0 || target_width > INT32_MAX ||
68
+ target_height <= 0 || target_height > INT32_MAX
69
+ ) {
70
+ err = js_throw_error(env, NULL, "Invalid target dimensions");
71
+ assert(err == 0);
72
+
73
+ return NULL;
74
+ }
75
+
76
+ uint64_t target_len = (uint64_t) target_width * (uint64_t) target_height * BYTES_PER_PIXEL;
77
+
78
+ if (target_len > SIZE_MAX) {
79
+ err = js_throw_error(env, NULL, "Target image too large");
80
+ assert(err == 0);
81
+
82
+ return NULL;
83
+ }
84
+
42
85
  js_value_t *result;
43
86
 
44
87
  uint8_t *target;
45
- err = js_create_unsafe_arraybuffer(env, target_width * target_height * 4, (void **) &target, &result);
88
+ err = js_create_unsafe_arraybuffer(env, (size_t) target_len, (void **) &target, &result);
46
89
  assert(err == 0);
47
90
 
48
91
  stbir_resize(
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.3",
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
  }