Skip to content

Commit e154ebc

Browse files
Daniel Ballahaesik
authored andcommitted
Add proper checks to udp module send (#1924)
Fixes #1904 IoT.js-DCO-1.0-Signed-off-by: Daniel Balla dballa@inf.u-szeged.hu
1 parent 6baab7e commit e154ebc

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

src/modules/iotjs_module_udp.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,22 @@ static void on_send(uv_udp_send_t* req, int status) {
183183
JS_FUNCTION(udp_send) {
184184
JS_DECLARE_PTR(jthis, uv_udp_t, udp_handle);
185185
DJS_CHECK_ARGS(3, object, number, string);
186-
IOTJS_ASSERT(jerry_value_is_function(jargv[3]) ||
187-
jerry_value_is_undefined(jargv[3]));
186+
187+
if (!jerry_value_is_undefined(jargv[3]) &&
188+
!jerry_value_is_function(jargv[3])) {
189+
return JS_CREATE_ERROR(TYPE, "Invalid callback given");
190+
}
188191

189192
const jerry_value_t jbuffer = JS_GET_ARG(0, object);
190193
const unsigned short port = JS_GET_ARG(1, number);
191194
iotjs_string_t address = JS_GET_ARG(2, string);
192195
jerry_value_t jcallback = JS_GET_ARG(3, object);
193196

194-
iotjs_bufferwrap_t* buffer_wrap = iotjs_bufferwrap_from_jbuffer(jbuffer);
197+
iotjs_bufferwrap_t* buffer_wrap = iotjs_jbuffer_get_bufferwrap_ptr(jbuffer);
198+
if (buffer_wrap == NULL) {
199+
return JS_CREATE_ERROR(TYPE, "Invalid buffer given");
200+
}
201+
195202
size_t len = iotjs_bufferwrap_length(buffer_wrap);
196203

197204
uv_req_t* req_send =

test/run_pass/issue/issue-1904.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* Copyright 2019-present Samsung Electronics Co., Ltd. and other contributors
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
var module = require('module');
17+
var dgram = require('dgram');
18+
var assert = require('assert');
19+
20+
try {
21+
dgram.createSocket('udp4')._handle.send(this, 0, '', new module());
22+
assert(false);
23+
} catch (e) {
24+
assert(e instanceof TypeError);
25+
}

test/testsets.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,12 @@
10321032
"required-modules": [
10331033
"websocket"
10341034
]
1035+
},
1036+
{
1037+
"name": "issue-1904.js",
1038+
"required-modules": [
1039+
"dgram"
1040+
]
10351041
}
10361042
],
10371043
"run_fail": [

0 commit comments

Comments
 (0)