@@ -704,7 +704,7 @@ static uv_write_t* uv__write(uv_stream_t* stream) {
704704
705705 assert (req -> write_index < req -> bufcnt );
706706
707- if (n < len ) {
707+ if (( size_t ) n < len ) {
708708 buf -> base += n ;
709709 buf -> len -= n ;
710710 stream -> write_queue_size -= n ;
@@ -717,7 +717,7 @@ static uv_write_t* uv__write(uv_stream_t* stream) {
717717 /* Finished writing the buf at index req->write_index. */
718718 req -> write_index ++ ;
719719
720- assert (n >= len );
720+ assert (( size_t ) n >= len );
721721 n -= len ;
722722
723723 assert (stream -> write_queue_size >= len );
@@ -1788,29 +1788,40 @@ int uv_pipe_init(uv_pipe_t* handle) {
17881788
17891789int uv_pipe_bind (uv_pipe_t * handle , const char * name ) {
17901790 struct sockaddr_un sun ;
1791+ const char * pipe_fname ;
17911792 int saved_errno ;
17921793 int sockfd ;
17931794 int status ;
17941795 int bound ;
17951796
17961797 saved_errno = errno ;
1798+ pipe_fname = NULL ;
17971799 sockfd = -1 ;
17981800 status = -1 ;
17991801 bound = 0 ;
18001802
1803+ /* Already bound? */
1804+ if (handle -> fd >= 0 ) {
1805+ uv_err_new_artificial ((uv_handle_t * )handle , UV_EINVAL );
1806+ goto out ;
1807+ }
1808+
18011809 /* Make a copy of the file name, it outlives this function's scope. */
1802- if ((name = ( const char * ) strdup (name )) == NULL ) {
1810+ if ((pipe_fname = strdup (name )) == NULL ) {
18031811 uv_err_new ((uv_handle_t * )handle , ENOMEM );
18041812 goto out ;
18051813 }
18061814
1815+ /* We've got a copy, don't touch the original any more. */
1816+ name = NULL ;
1817+
18071818 if ((sockfd = uv__socket (AF_UNIX , SOCK_STREAM , 0 )) == -1 ) {
18081819 uv_err_new ((uv_handle_t * )handle , errno );
18091820 goto out ;
18101821 }
18111822
18121823 memset (& sun , 0 , sizeof sun );
1813- uv__strlcpy (sun .sun_path , name , sizeof (sun .sun_path ));
1824+ uv__strlcpy (sun .sun_path , pipe_fname , sizeof (sun .sun_path ));
18141825 sun .sun_family = AF_UNIX ;
18151826
18161827 if (bind (sockfd , (struct sockaddr * )& sun , sizeof sun ) == -1 ) {
@@ -1820,24 +1831,25 @@ int uv_pipe_bind(uv_pipe_t* handle, const char* name) {
18201831 * on EADDRINUSE. Unlinking and trying to bind again opens
18211832 * a window for races with other threads and processes.
18221833 */
1823- uv_err_new ((uv_handle_t * )handle , errno );
1834+ uv_err_new ((uv_handle_t * )handle , ( errno == ENOENT ) ? EACCES : errno );
18241835 goto out ;
18251836#else
18261837 /*
18271838 * Try to re-purpose the socket. This is a potential race window.
18281839 */
18291840 if (errno != EADDRINUSE
1830- || unlink (name ) == -1
1841+ || unlink (pipe_fname ) == -1
18311842 || bind (sockfd , (struct sockaddr * )& sun , sizeof sun ) == -1 ) {
1832- uv_err_new ((uv_handle_t * )handle , errno );
1843+ /* Convert ENOENT to EACCES for compatibility with Windows. */
1844+ uv_err_new ((uv_handle_t * )handle , (errno == ENOENT ) ? EACCES : errno );
18331845 goto out ;
18341846 }
18351847#endif
18361848 }
18371849 bound = 1 ;
18381850
18391851 /* Success. */
1840- handle -> pipe_fname = name ; /* Is a strdup'ed copy. */
1852+ handle -> pipe_fname = pipe_fname ; /* Is a strdup'ed copy. */
18411853 handle -> fd = sockfd ;
18421854 status = 0 ;
18431855
@@ -1846,10 +1858,11 @@ int uv_pipe_bind(uv_pipe_t* handle, const char* name) {
18461858 if (status ) {
18471859 if (bound ) {
18481860 /* unlink() before close() to avoid races. */
1849- unlink (name );
1861+ assert (pipe_fname != NULL );
1862+ unlink (pipe_fname );
18501863 }
18511864 uv__close (sockfd );
1852- free ((void * )name );
1865+ free ((void * )pipe_fname );
18531866 }
18541867
18551868 errno = saved_errno ;
@@ -1862,6 +1875,13 @@ int uv_pipe_listen(uv_pipe_t* handle, uv_connection_cb cb) {
18621875 int status ;
18631876
18641877 saved_errno = errno ;
1878+ status = -1 ;
1879+
1880+ if (handle -> fd == -1 ) {
1881+ uv_err_new_artificial ((uv_handle_t * )handle , UV_ENOTCONN );
1882+ goto out ;
1883+ }
1884+ assert (handle -> fd >= 0 );
18651885
18661886 if ((status = listen (handle -> fd , SOMAXCONN )) == -1 ) {
18671887 uv_err_new ((uv_handle_t * )handle , errno );
@@ -1871,6 +1891,7 @@ int uv_pipe_listen(uv_pipe_t* handle, uv_connection_cb cb) {
18711891 ev_io_start (EV_DEFAULT_ & handle -> read_watcher );
18721892 }
18731893
1894+ out :
18741895 errno = saved_errno ;
18751896 return status ;
18761897}
0 commit comments