@@ -3920,6 +3920,7 @@ VOID _nx_http_server_put_process(NX_HTTP_SERVER *server_ptr, NX_PACKET *packet_
39203920
39213921UINT status ;
39223922ULONG length ;
3923+ ULONG consumed_length = 0 ;
39233924UINT offset ;
39243925CHAR * name_ptr ;
39253926CHAR * password_ptr ;
@@ -4115,8 +4116,8 @@ UINT temp_realm_length = 0;
41154116 _nx_http_server_response_send (server_ptr , NX_HTTP_STATUS_INTERNAL_ERROR , sizeof (NX_HTTP_STATUS_INTERNAL_ERROR ) - 1 ,
41164117 "NetX HTTP File Create Failed" , sizeof ("NetX HTTP File Create Failed" ) - 1 , NX_NULL , 0 );
41174118
4118- /* Error, return to caller. */
4119- return ;
4119+ /* Error, return to caller. */
4120+ goto put_process_end ;
41204121 }
41214122
41224123 /* Open the specified file for writing. */
@@ -4130,8 +4131,8 @@ UINT temp_realm_length = 0;
41304131 _nx_http_server_response_send (server_ptr , NX_HTTP_STATUS_INTERNAL_ERROR , sizeof (NX_HTTP_STATUS_INTERNAL_ERROR ) - 1 ,
41314132 "NetX HTTP File Open Failed" , sizeof ("NetX HTTP File Open Failed" ) - 1 , NX_NULL , 0 );
41324133
4133- /* Error, return to caller. */
4134- return ;
4134+ /* Error, return to caller. */
4135+ goto put_process_end ;
41354136 }
41364137
41374138
@@ -4151,12 +4152,27 @@ UINT temp_realm_length = 0;
41514152 _nx_http_server_response_send (server_ptr , NX_HTTP_STATUS_INTERNAL_ERROR , sizeof (NX_HTTP_STATUS_INTERNAL_ERROR ) - 1 ,
41524153 "NetX HTTP File Write Failed" , sizeof ("NetX HTTP File Write Failed" ) - 1 , NX_NULL , 0 );
41534154
4154- /* Error, return to caller. */
4155- return ;
4155+ /* Error, return to caller. */
4156+ goto put_process_end ;
41564157 }
41574158
41584159 /* Update the length. */
4159- length = length - ((ULONG )(packet_ptr -> nx_packet_append_ptr - packet_ptr -> nx_packet_prepend_ptr ) - offset );
4160+ consumed_length = ((ULONG )(packet_ptr -> nx_packet_append_ptr - packet_ptr -> nx_packet_prepend_ptr ) - offset );
4161+ if ((length - consumed_length ) > length )
4162+ {
4163+ /* Underflow error has occurred. */
4164+
4165+ /* Send response back to client. */
4166+ _nx_http_server_response_send (server_ptr , NX_HTTP_STATUS_BAD_REQUEST ,
4167+ sizeof (NX_HTTP_STATUS_BAD_REQUEST ) - 1 ,
4168+ "NetX HTTP Content Length" ,
4169+ sizeof ("NetX HTTP Content Length" ) - 1 ,
4170+ NX_NULL , 0 );
4171+
4172+ status = NX_UNDERFLOW ;
4173+ goto put_process_end ;
4174+ }
4175+ length -= consumed_length ;
41604176
41614177 /* Increment the bytes received count. */
41624178 server_ptr -> nx_http_server_total_bytes_received = server_ptr -> nx_http_server_total_bytes_received +
@@ -4183,11 +4199,26 @@ UINT temp_realm_length = 0;
41834199 "NetX HTTP File Write Failed" , sizeof ("NetX HTTP File Write Failed" ) - 1 , NX_NULL , 0 );
41844200
41854201 /* Error, return to caller. */
4186- return ;
4202+ goto put_process_end ;
41874203 }
41884204
41894205 /* Update the length. */
4190- length = length - (ULONG )(next_packet_ptr -> nx_packet_append_ptr - next_packet_ptr -> nx_packet_prepend_ptr );
4206+ consumed_length = (ULONG )(next_packet_ptr -> nx_packet_append_ptr - next_packet_ptr -> nx_packet_prepend_ptr );
4207+ if ((length - consumed_length ) > length )
4208+ {
4209+ /* Underflow error has occurred. */
4210+
4211+ /* Send response back to client. */
4212+ _nx_http_server_response_send (server_ptr , NX_HTTP_STATUS_BAD_REQUEST ,
4213+ sizeof (NX_HTTP_STATUS_BAD_REQUEST ) - 1 ,
4214+ "NetX HTTP Content Length" ,
4215+ sizeof ("NetX HTTP Content Length" ) - 1 ,
4216+ NX_NULL , 0 );
4217+
4218+ status = NX_UNDERFLOW ;
4219+ goto put_process_end ;
4220+ }
4221+ length -= consumed_length ;
41914222
41924223 /* Increment the bytes received count. */
41934224 server_ptr -> nx_http_server_total_bytes_received = server_ptr -> nx_http_server_total_bytes_received +
@@ -4214,7 +4245,7 @@ UINT temp_realm_length = 0;
42144245 "NetX HTTP Receive Timeout" , sizeof ("NetX HTTP Receive Timeout" ) - 1 , NX_NULL , 0 );
42154246
42164247 /* Error, return to caller. */
4217- return ;
4248+ goto put_process_end ;
42184249 }
42194250
42204251 /* Loop to write the packet chain out to the file. */
@@ -4238,11 +4269,29 @@ UINT temp_realm_length = 0;
42384269 nx_packet_release (data_packet_ptr );
42394270
42404271 /* Error, return to caller. */
4241- return ;
4272+ goto put_process_end ;
42424273 }
42434274
42444275 /* Update the length. */
4245- length = length - (UINT )(next_packet_ptr -> nx_packet_append_ptr - next_packet_ptr -> nx_packet_prepend_ptr );
4276+ consumed_length = (ULONG )(next_packet_ptr -> nx_packet_append_ptr - next_packet_ptr -> nx_packet_prepend_ptr );
4277+ if ((length - consumed_length ) > length )
4278+ {
4279+ /* Underflow error has occurred. */
4280+
4281+ /* Send response back to client. */
4282+ _nx_http_server_response_send (server_ptr , NX_HTTP_STATUS_BAD_REQUEST ,
4283+ sizeof (NX_HTTP_STATUS_BAD_REQUEST ) - 1 ,
4284+ "NetX HTTP Content Length" ,
4285+ sizeof ("NetX HTTP Content Length" ) - 1 ,
4286+ NX_NULL , 0 );
4287+
4288+ /* Release the previous data packet. */
4289+ nx_packet_release (data_packet_ptr );
4290+
4291+ status = NX_UNDERFLOW ;
4292+ goto put_process_end ;
4293+ }
4294+ length -= consumed_length ;
42464295
42474296 /* Increment the bytes received count. */
42484297 server_ptr -> nx_http_server_total_bytes_received = server_ptr -> nx_http_server_total_bytes_received +
@@ -4261,11 +4310,9 @@ UINT temp_realm_length = 0;
42614310 nx_packet_release (data_packet_ptr );
42624311 }
42634312
4264- /* Success, at this point close the file and prepare a successful response for the client. */
4265- fx_file_close (& (server_ptr -> nx_http_server_file ));
4266-
4267-
4268- /* Now build a response header. */
4313+ /* Now build a response header. There is no need to check for success up to this
4314+ point. If an error has occurred earlier in this function, execution will have
4315+ jumped straight to the put_process_end label. */
42694316 status = _nx_http_server_generate_response_header (server_ptr , & data_packet_ptr , NX_HTTP_STATUS_OK , sizeof (NX_HTTP_STATUS_OK ) - 1 , 0 , NX_NULL , 0 , NX_NULL , 0 );
42704317 if (status == NX_SUCCESS )
42714318 {
@@ -4282,6 +4329,9 @@ UINT temp_realm_length = 0;
42824329 }
42834330 }
42844331
4332+ put_process_end :
4333+ /* Always attempt to clean up by closing the file. */
4334+ fx_file_close (& (server_ptr -> nx_http_server_file ));
42854335}
42864336
42874337
0 commit comments