Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(241)

Side by Side Diff: runtime/bin/socket.cc

Issue 91043003: Simplify some argument checking for socket calls (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed review comments Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/bin/dartutils.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "bin/io_buffer.h" 5 #include "bin/io_buffer.h"
6 #include "bin/socket.h" 6 #include "bin/socket.h"
7 #include "bin/dartutils.h" 7 #include "bin/dartutils.h"
8 #include "bin/thread.h" 8 #include "bin/thread.h"
9 #include "bin/utils.h" 9 #include "bin/utils.h"
10 10
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 Dart_SetReturnValue(args, Dart_Null()); 96 Dart_SetReturnValue(args, Dart_Null());
97 } else { 97 } else {
98 Dart_SetReturnValue(args, SocketAddress::ToTypedData(&raw)); 98 Dart_SetReturnValue(args, SocketAddress::ToTypedData(&raw));
99 } 99 }
100 } 100 }
101 101
102 102
103 void FUNCTION_NAME(Socket_CreateConnect)(Dart_NativeArguments args) { 103 void FUNCTION_NAME(Socket_CreateConnect)(Dart_NativeArguments args) {
104 RawAddr addr; 104 RawAddr addr;
105 GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); 105 GetSockAddr(Dart_GetNativeArgument(args, 1), &addr);
106 int64_t port = 0; 106 int64_t port = DartUtils::GetInt64ValueCheckRange(
107 if (DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 2), &port)) { 107 Dart_GetNativeArgument(args, 2),
108 intptr_t socket = Socket::CreateConnect(addr, port); 108 0,
109 OSError error; 109 65535);
110 if (socket >= 0) { 110 intptr_t socket = Socket::CreateConnect(addr, port);
111 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket); 111 OSError error;
112 Dart_SetReturnValue(args, Dart_True()); 112 if (socket >= 0) {
113 } else { 113 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket);
114 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error)); 114 Dart_SetReturnValue(args, Dart_True());
115 }
116 } else { 115 } else {
117 OSError os_error(-1, "Invalid argument", OSError::kUnknown); 116 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error));
118 Dart_Handle err = DartUtils::NewDartOSError(&os_error);
119 if (Dart_IsError(err)) Dart_PropagateError(err);
120 Dart_SetReturnValue(args, err);
121 } 117 }
122 } 118 }
123 119
124 120
125 void FUNCTION_NAME(Socket_Available)(Dart_NativeArguments args) { 121 void FUNCTION_NAME(Socket_Available)(Dart_NativeArguments args) {
126 intptr_t socket = 122 intptr_t socket =
127 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); 123 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0));
128 intptr_t available = Socket::Available(socket); 124 intptr_t available = Socket::Available(socket);
129 if (available >= 0) { 125 if (available >= 0) {
130 Dart_SetReturnValue(args, Dart_NewInteger(available)); 126 Dart_SetReturnValue(args, Dart_NewInteger(available));
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 intptr_t type = Socket::GetType(socket); 274 intptr_t type = Socket::GetType(socket);
279 if (type >= 0) { 275 if (type >= 0) {
280 Dart_SetReturnValue(args, Dart_NewInteger(type)); 276 Dart_SetReturnValue(args, Dart_NewInteger(type));
281 } else { 277 } else {
282 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); 278 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
283 } 279 }
284 } 280 }
285 281
286 282
287 void FUNCTION_NAME(Socket_GetStdioHandle)(Dart_NativeArguments args) { 283 void FUNCTION_NAME(Socket_GetStdioHandle)(Dart_NativeArguments args) {
288 intptr_t num = 284 int64_t num = DartUtils::GetInt64ValueCheckRange(
289 DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 1)); 285 Dart_GetNativeArgument(args, 1), 0, 2);
290 ASSERT(num == 0 || num == 1 || num == 2);
291 intptr_t socket = Socket::GetStdioHandle(num); 286 intptr_t socket = Socket::GetStdioHandle(num);
292 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket); 287 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket);
293 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0)); 288 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0));
294 } 289 }
295 290
296 291
297 void FUNCTION_NAME(Socket_SetSocketId)(Dart_NativeArguments args) { 292 void FUNCTION_NAME(Socket_SetSocketId)(Dart_NativeArguments args) {
298 intptr_t id = 293 intptr_t id =
299 DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 1)); 294 DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 1));
300 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), id); 295 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), id);
301 } 296 }
302 297
303 298
304 void FUNCTION_NAME(ServerSocket_CreateBindListen)(Dart_NativeArguments args) { 299 void FUNCTION_NAME(ServerSocket_CreateBindListen)(Dart_NativeArguments args) {
305 RawAddr addr; 300 RawAddr addr;
306 int64_t port = 0;
307 int64_t backlog = 0;
308 GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); 301 GetSockAddr(Dart_GetNativeArgument(args, 1), &addr);
309 if (DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 2), &port) && 302 int64_t port = DartUtils::GetInt64ValueCheckRange(
310 DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 3), &backlog)) { 303 Dart_GetNativeArgument(args, 2),
311 bool v6_only = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 4)); 304 0,
312 intptr_t socket = ServerSocket::CreateBindListen( 305 65535);
313 addr, port, backlog, v6_only); 306 int64_t backlog = DartUtils::GetInt64ValueCheckRange(
314 OSError error; 307 Dart_GetNativeArgument(args, 3),
315 if (socket >= 0) { 308 0,
316 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket); 309 65535);
317 Dart_SetReturnValue(args, Dart_True()); 310 bool v6_only = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 4));
311 intptr_t socket = ServerSocket::CreateBindListen(
312 addr, port, backlog, v6_only);
313 OSError error;
314 if (socket >= 0) {
315 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket);
316 Dart_SetReturnValue(args, Dart_True());
317 } else {
318 if (socket == -5) {
319 OSError os_error(-1, "Invalid host", OSError::kUnknown);
320 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error));
318 } else { 321 } else {
319 if (socket == -5) { 322 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error));
320 OSError os_error(-1, "Invalid host", OSError::kUnknown);
321 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error));
322 } else {
323 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error));
324 }
325 } 323 }
326 } else {
327 OSError os_error(-1, "Invalid argument", OSError::kUnknown);
328 Dart_Handle err = DartUtils::NewDartOSError(&os_error);
329 if (Dart_IsError(err)) Dart_PropagateError(err);
330 Dart_SetReturnValue(args, err);
331 } 324 }
332 } 325 }
333 326
334 327
335 void FUNCTION_NAME(ServerSocket_Accept)(Dart_NativeArguments args) { 328 void FUNCTION_NAME(ServerSocket_Accept)(Dart_NativeArguments args) {
336 intptr_t socket = 329 intptr_t socket =
337 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); 330 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0));
338 intptr_t new_socket = ServerSocket::Accept(socket); 331 intptr_t new_socket = ServerSocket::Accept(socket);
339 if (new_socket >= 0) { 332 if (new_socket >= 0) {
340 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 1), new_socket); 333 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 1), new_socket);
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
503 intptr_t Socket::GetSocketIdNativeField(Dart_Handle socket_obj) { 496 intptr_t Socket::GetSocketIdNativeField(Dart_Handle socket_obj) {
504 intptr_t socket = 0; 497 intptr_t socket = 0;
505 Dart_Handle err = 498 Dart_Handle err =
506 Dart_GetNativeInstanceField(socket_obj, kSocketIdNativeField, &socket); 499 Dart_GetNativeInstanceField(socket_obj, kSocketIdNativeField, &socket);
507 if (Dart_IsError(err)) Dart_PropagateError(err); 500 if (Dart_IsError(err)) Dart_PropagateError(err);
508 return socket; 501 return socket;
509 } 502 }
510 503
511 } // namespace bin 504 } // namespace bin
512 } // namespace dart 505 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/bin/dartutils.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698