2004-08-16 Dick Porter <dick@ximian.com>
authorDick Porter <dick@acm.org>
Mon, 16 Aug 2004 16:24:02 +0000 (16:24 -0000)
committerDick Porter <dick@acm.org>
Mon, 16 Aug 2004 16:24:02 +0000 (16:24 -0000)
* sockets.c:
* io.c:
* handles-private.h (_wapi_handle_fd_offset_to_handle): Improve
error checking with passed-in file descriptors.

svn path=/trunk/mono/; revision=32390

mono/io-layer/ChangeLog
mono/io-layer/handles-private.h
mono/io-layer/io.c
mono/io-layer/sockets.c

index 5dc428485eef10172ec1954b2311faa2f53fa91b..670e10df191b57019fd93143ada6770b842e4959 100644 (file)
@@ -1,3 +1,10 @@
+2004-08-16  Dick Porter  <dick@ximian.com>
+
+       * sockets.c:
+       * io.c:
+       * handles-private.h (_wapi_handle_fd_offset_to_handle): Improve
+       error checking with passed-in file descriptors.
+
 2004-08-11  Dick Porter  <dick@ximian.com>
 
        * sockets.c: 
index c3043461c54bc31e22809e97c6207f744bf88e35..36890a97160b60f467c66272abdf35ed27acfa88 100644 (file)
@@ -113,17 +113,24 @@ static inline void _wapi_handle_fd_offset_store (int fd, gpointer handle)
 static inline gpointer _wapi_handle_fd_offset_to_handle (gpointer fd_handle)
 {
        int fd = GPOINTER_TO_INT (fd_handle);
+       gpointer handle;
        
-       g_assert (fd < _wapi_fd_offset_table_size);
-       g_assert (_wapi_fd_offset_table[fd]!=NULL);
-       g_assert (GPOINTER_TO_UINT (_wapi_fd_offset_table[fd]) >= _wapi_fd_offset_table_size);
+       if (fd >= _wapi_fd_offset_table_size) {
+               return(NULL);
+       }
+       
+       handle = _wapi_fd_offset_table[fd];
+       
+       if (GPOINTER_TO_UINT (handle) < _wapi_fd_offset_table_size) {
+               return(NULL);
+       }
 
 #ifdef DEBUG
        g_message (G_GNUC_PRETTY_FUNCTION ": Returning fd offset %d of %p", fd,
-                  _wapi_fd_offset_table[fd]);
+                  handle);
 #endif
 
-       return(_wapi_fd_offset_table[fd]);
+       return(handle);
 }
 
 static inline struct _WapiHandleShared_list *_wapi_handle_get_shared_segment (guint32 segment)
index e369e0cd0bd619a7f309d3fa8f6d264fb2783a58..c5cd7f10110f14dae6e9be7fe4cbd2036fe41507 100644 (file)
@@ -2167,9 +2167,17 @@ gboolean ReadFile(gpointer fd_handle, gpointer buffer, guint32 numbytes,
                  guint32 *bytesread, WapiOverlapped *overlapped)
 {
        gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
-       WapiHandleType type=_wapi_handle_type (handle);
+       WapiHandleType type;
+
+       if (handle == NULL) {
+               SetLastError (ERROR_INVALID_HANDLE);
+               return(FALSE);
+       }
+       
+       type = _wapi_handle_type (handle);
        
        if(io_ops[type].readfile==NULL) {
+               SetLastError (ERROR_INVALID_HANDLE);
                return(FALSE);
        }
        
@@ -2206,9 +2214,17 @@ gboolean WriteFile(gpointer fd_handle, gconstpointer buffer, guint32 numbytes,
                   guint32 *byteswritten, WapiOverlapped *overlapped)
 {
        gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
-       WapiHandleType type=_wapi_handle_type (handle);
+       WapiHandleType type;
+
+       if (handle == NULL) {
+               SetLastError (ERROR_INVALID_HANDLE);
+               return(FALSE);
+       }
+       
+       type = _wapi_handle_type (handle);
        
        if(io_ops[type].writefile==NULL) {
+               SetLastError (ERROR_INVALID_HANDLE);
                return(FALSE);
        }
        
@@ -2229,9 +2245,17 @@ gboolean WriteFile(gpointer fd_handle, gconstpointer buffer, guint32 numbytes,
 gboolean FlushFileBuffers(gpointer fd_handle)
 {
        gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
-       WapiHandleType type=_wapi_handle_type (handle);
+       WapiHandleType type;
+
+       if (handle == NULL) {
+               SetLastError (ERROR_INVALID_HANDLE);
+               return(FALSE);
+       }
+       
+       type = _wapi_handle_type (handle);
        
        if(io_ops[type].flushfile==NULL) {
+               SetLastError (ERROR_INVALID_HANDLE);
                return(FALSE);
        }
        
@@ -2251,9 +2275,17 @@ gboolean FlushFileBuffers(gpointer fd_handle)
 gboolean SetEndOfFile(gpointer fd_handle)
 {
        gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
-       WapiHandleType type=_wapi_handle_type (handle);
+       WapiHandleType type;
+
+       if (handle == NULL) {
+               SetLastError (ERROR_INVALID_HANDLE);
+               return(FALSE);
+       }
+       
+       type = _wapi_handle_type (handle);
        
        if(io_ops[type].setendoffile==NULL) {
+               SetLastError (ERROR_INVALID_HANDLE);
                return(FALSE);
        }
        
@@ -2293,10 +2325,18 @@ guint32 SetFilePointer(gpointer fd_handle, gint32 movedistance,
                       gint32 *highmovedistance, WapiSeekMethod method)
 {
        gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
-       WapiHandleType type=_wapi_handle_type (handle);
+       WapiHandleType type;
+
+       if (handle == NULL) {
+               SetLastError (ERROR_INVALID_HANDLE);
+               return(INVALID_SET_FILE_POINTER);
+       }
+       
+       type = _wapi_handle_type (handle);
        
        if(io_ops[type].seek==NULL) {
-               return(FALSE);
+               SetLastError (ERROR_INVALID_HANDLE);
+               return(INVALID_SET_FILE_POINTER);
        }
        
        return(io_ops[type].seek (handle, movedistance, highmovedistance,
@@ -2317,9 +2357,17 @@ guint32 SetFilePointer(gpointer fd_handle, gint32 movedistance,
 WapiFileType GetFileType(gpointer fd_handle)
 {
        gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
-       WapiHandleType type=_wapi_handle_type (handle);
+       WapiHandleType type;
+
+       if (handle == NULL) {
+               SetLastError (ERROR_INVALID_HANDLE);
+               return(FILE_TYPE_UNKNOWN);
+       }
+       
+       type = _wapi_handle_type (handle);
        
        if(io_ops[type].getfiletype==NULL) {
+               SetLastError (ERROR_INVALID_HANDLE);
                return(FILE_TYPE_UNKNOWN);
        }
        
@@ -2345,10 +2393,18 @@ WapiFileType GetFileType(gpointer fd_handle)
 guint32 GetFileSize(gpointer fd_handle, guint32 *highsize)
 {
        gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
-       WapiHandleType type=_wapi_handle_type (handle);
+       WapiHandleType type;
+
+       if (handle == NULL) {
+               SetLastError (ERROR_INVALID_HANDLE);
+               return(INVALID_FILE_SIZE);
+       }
+       
+       type = _wapi_handle_type (handle);
        
        if(io_ops[type].getfilesize==NULL) {
-               return(FALSE);
+               SetLastError (ERROR_INVALID_HANDLE);
+               return(INVALID_FILE_SIZE);
        }
        
        return(io_ops[type].getfilesize (handle, highsize));
@@ -2383,9 +2439,17 @@ gboolean GetFileTime(gpointer fd_handle, WapiFileTime *create_time,
                     WapiFileTime *last_access, WapiFileTime *last_write)
 {
        gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
-       WapiHandleType type=_wapi_handle_type (handle);
+       WapiHandleType type;
+
+       if (handle == NULL) {
+               SetLastError (ERROR_INVALID_HANDLE);
+               return(FALSE);
+       }
+       
+       type = _wapi_handle_type (handle);
        
        if(io_ops[type].getfiletime==NULL) {
+               SetLastError (ERROR_INVALID_HANDLE);
                return(FALSE);
        }
        
@@ -2421,9 +2485,17 @@ gboolean SetFileTime(gpointer fd_handle, const WapiFileTime *create_time,
                     const WapiFileTime *last_write)
 {
        gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
-       WapiHandleType type=_wapi_handle_type (handle);
+       WapiHandleType type;
+
+       if (handle == NULL) {
+               SetLastError (ERROR_INVALID_HANDLE);
+               return(FALSE);
+       }
+       
+       type = _wapi_handle_type (handle);
        
        if(io_ops[type].setfiletime==NULL) {
+               SetLastError (ERROR_INVALID_HANDLE);
                return(FALSE);
        }
        
@@ -3651,6 +3723,11 @@ _wapi_io_add_callback (gpointer fd_handle,
        gboolean ret = FALSE;
        gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
        
+       if (handle == NULL) {
+               SetLastError (ERROR_INVALID_HANDLE);
+               return(FALSE);
+       }
+       
        ok = _wapi_lookup_handle (handle, WAPI_HANDLE_FILE,
                                  (gpointer *) &file_handle,
                                  (gpointer *) &file_private_handle);
@@ -3831,6 +3908,11 @@ gboolean LockFile (gpointer fd_handle, guint32 offset_low, guint32 offset_high,
        gboolean ok;
        off_t offset, length;
        gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
+
+       if (handle == NULL) {
+               SetLastError (ERROR_INVALID_HANDLE);
+               return(FALSE);
+       }
        
        ok = _wapi_lookup_handle (handle, WAPI_HANDLE_FILE,
                                  (gpointer *)&file_handle,
@@ -3885,6 +3967,11 @@ gboolean UnlockFile (gpointer fd_handle, guint32 offset_low,
        gboolean ok;
        off_t offset, length;
        gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
+
+       if (handle == NULL) {
+               SetLastError (ERROR_INVALID_HANDLE);
+               return(FALSE);
+       }
        
        ok = _wapi_lookup_handle (handle, WAPI_HANDLE_FILE,
                                  (gpointer *)&file_handle,
index 46531f2d75d6b477d170a67b697f77587240abb9..485d69e5a744d27f7fbb6d6eee3dc7204a415c19 100644 (file)
@@ -212,6 +212,12 @@ int closesocket(guint32 fd_handle)
 {
        gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd_handle));
        
+       if (handle == NULL ||
+           _wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+               WSASetLastError (WSAENOTSOCK);
+               return(0);
+       }
+       
        _wapi_handle_unref (handle);
        return(0);
 }
@@ -231,7 +237,8 @@ guint32 _wapi_accept(guint32 fd, struct sockaddr *addr, socklen_t *addrlen)
                return(INVALID_SOCKET);
        }
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (handle == NULL ||
+           _wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return(INVALID_SOCKET);
        }
@@ -305,7 +312,8 @@ int _wapi_bind(guint32 fd, struct sockaddr *my_addr, socklen_t addrlen)
                return(SOCKET_ERROR);
        }
 
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (handle == NULL ||
+           _wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
@@ -337,7 +345,8 @@ int _wapi_connect(guint32 fd, const struct sockaddr *serv_addr,
                return(SOCKET_ERROR);
        }
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (handle == NULL ||
+           _wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
@@ -390,7 +399,8 @@ int _wapi_getpeername(guint32 fd, struct sockaddr *name, socklen_t *namelen)
                return(SOCKET_ERROR);
        }
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (handle == NULL ||
+           _wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
@@ -422,7 +432,8 @@ int _wapi_getsockname(guint32 fd, struct sockaddr *name, socklen_t *namelen)
                return(SOCKET_ERROR);
        }
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (handle == NULL ||
+           _wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
@@ -455,7 +466,8 @@ int _wapi_getsockopt(guint32 fd, int level, int optname, void *optval,
                return(SOCKET_ERROR);
        }
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (handle == NULL ||
+           _wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
@@ -487,7 +499,8 @@ int _wapi_listen(guint32 fd, int backlog)
                return(SOCKET_ERROR);
        }
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (handle == NULL ||
+           _wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
@@ -528,7 +541,8 @@ int _wapi_recvfrom(guint32 fd, void *buf, size_t len, int recv_flags,
                return(SOCKET_ERROR);
        }
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (handle == NULL ||
+           _wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
@@ -576,7 +590,8 @@ int _wapi_send(guint32 fd, const void *msg, size_t len, int send_flags)
                return(SOCKET_ERROR);
        }
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (handle == NULL ||
+           _wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
@@ -623,7 +638,8 @@ int _wapi_sendto(guint32 fd, const void *msg, size_t len, int send_flags,
                return(SOCKET_ERROR);
        }
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (handle == NULL ||
+           _wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
@@ -667,7 +683,8 @@ int _wapi_setsockopt(guint32 fd, int level, int optname,
                return(SOCKET_ERROR);
        }
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (handle == NULL ||
+           _wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
@@ -699,7 +716,8 @@ int _wapi_shutdown(guint32 fd, int how)
                return(SOCKET_ERROR);
        }
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (handle == NULL ||
+           _wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
@@ -844,7 +862,8 @@ WSAIoctl (guint32 fd, gint32 command,
                return(SOCKET_ERROR);
        }
 
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (handle == NULL ||
+           _wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError (WSAENOTSOCK);
                return SOCKET_ERROR;
        }
@@ -891,7 +910,8 @@ int ioctlsocket(guint32 fd, gint32 command, gpointer arg)
                return(SOCKET_ERROR);
        }
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (handle == NULL ||
+           _wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return(SOCKET_ERROR);
        }
@@ -975,7 +995,8 @@ void _wapi_FD_CLR(guint32 fd, fd_set *set)
 {
        gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (handle == NULL ||
+           _wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return;
        }
@@ -987,7 +1008,8 @@ int _wapi_FD_ISSET(guint32 fd, fd_set *set)
 {
        gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (handle == NULL ||
+           _wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return(0);
        }
@@ -999,7 +1021,8 @@ void _wapi_FD_SET(guint32 fd, fd_set *set)
 {
        gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (handle == NULL ||
+           _wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError(WSAENOTSOCK);
                return;
        }
@@ -1050,7 +1073,8 @@ do_aio_call (gboolean is_read, gpointer fd_handle, gpointer buffer,
        int result;
        notifier_data_t *ndata;
        
-       if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
+       if (handle == NULL ||
+           _wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
                WSASetLastError (WSAENOTSOCK);
                return FALSE;
        }