@@ -102,67 +102,81 @@ private unsafe static int Read(
102102 UIntPtr buf_size ,
103103 out UIntPtr bytes_read )
104104 {
105- GitErrorCode errorCode = GitErrorCode . Error ;
106105 bytes_read = UIntPtr . Zero ;
107106
108107 SmartSubtransportStream transportStream =
109108 GCHandle . FromIntPtr ( Marshal . ReadIntPtr ( stream , GitSmartSubtransportStream . GCHandleOffset ) ) . Target as SmartSubtransportStream ;
110109
111- if ( transportStream != null &&
112- buf_size . ToUInt64 ( ) < ( ulong ) long . MaxValue )
110+ if ( transportStream == null )
111+ {
112+ Proxy . git_error_set_str ( GitErrorCategory . Net , "no transport stream provided" ) ;
113+ return ( int ) GitErrorCode . Error ;
114+ }
115+
116+ if ( buf_size . ToUInt64 ( ) >= ( ulong ) long . MaxValue )
117+ {
118+ Proxy . git_error_set_str ( GitErrorCategory . Net , "buffer size is too large" ) ;
119+ return ( int ) GitErrorCode . Error ;
120+ }
121+
122+ try
113123 {
114124 using ( UnmanagedMemoryStream memoryStream = new UnmanagedMemoryStream ( ( byte * ) buffer , 0 ,
115125 ( long ) buf_size . ToUInt64 ( ) ,
116126 FileAccess . ReadWrite ) )
117127 {
118- try
119- {
120- long longBytesRead ;
121-
122- int toReturn = transportStream . Read ( memoryStream , ( long ) buf_size . ToUInt64 ( ) , out longBytesRead ) ;
123-
124- bytes_read = new UIntPtr ( ( ulong ) Math . Max ( 0 , longBytesRead ) ) ;
125-
126- return toReturn ;
127- }
128- catch ( NativeException ex )
129- {
130- errorCode = ex . ErrorCode ;
131- Proxy . giterr_set_str ( GitErrorCategory . Net , ex ) ;
132- }
133- catch ( Exception ex )
134- {
135- Proxy . git_error_set_str ( GitErrorCategory . Net , ex ) ;
136- }
128+ long longBytesRead ;
129+
130+ int toReturn = transportStream . Read ( memoryStream , ( long ) buf_size . ToUInt64 ( ) , out longBytesRead ) ;
131+
132+ bytes_read = new UIntPtr ( ( ulong ) Math . Max ( 0 , longBytesRead ) ) ;
133+
134+ return toReturn ;
137135 }
138136 }
139-
140- return ( int ) errorCode ;
137+ catch ( NativeException ex )
138+ {
139+ Proxy . git_error_set_str ( GitErrorCategory . Net , ex ) ;
140+ return ( int ) ex . ErrorCode ;
141+ }
142+ catch ( Exception ex )
143+ {
144+ Proxy . git_error_set_str ( GitErrorCategory . Net , ex ) ;
145+ return ( int ) GitErrorCode . Error ;
146+ }
141147 }
142148
143149 private static unsafe int Write ( IntPtr stream , IntPtr buffer , UIntPtr len )
144150 {
145151 SmartSubtransportStream transportStream =
146152 GCHandle . FromIntPtr ( Marshal . ReadIntPtr ( stream , GitSmartSubtransportStream . GCHandleOffset ) ) . Target as SmartSubtransportStream ;
147153
148- if ( transportStream != null && len . ToUInt64 ( ) < ( ulong ) long . MaxValue )
154+ if ( transportStream == null )
155+ {
156+ Proxy . git_error_set_str ( GitErrorCategory . Net , "no transport stream provided" ) ;
157+ return ( int ) GitErrorCode . Error ;
158+ }
159+
160+ if ( len . ToUInt64 ( ) >= ( ulong ) long . MaxValue )
161+ {
162+ Proxy . git_error_set_str ( GitErrorCategory . Net , "write length is too large" ) ;
163+ return ( int ) GitErrorCode . Error ;
164+ }
165+
166+ try
149167 {
150168 long length = ( long ) len . ToUInt64 ( ) ;
151169
152170 using ( UnmanagedMemoryStream dataStream = new UnmanagedMemoryStream ( ( byte * ) buffer , length ) )
153171 {
154- try
155- {
156- return transportStream . Write ( dataStream , length ) ;
157- }
158- catch ( Exception ex )
159- {
160- Proxy . git_error_set_str ( GitErrorCategory . Net , ex ) ;
161- }
172+ return transportStream . Write ( dataStream , length ) ;
162173 }
163174 }
164-
165- return ( int ) GitErrorCode . Error ;
175+ catch ( Exception ex )
176+ {
177+ Proxy . git_error_set_str ( GitErrorCategory . Net , ex ) ;
178+ return ( int ) GitErrorCode . Error ;
179+ }
166180 }
167181
168182 private static void Free ( IntPtr stream )
0 commit comments