Skip to content

Commit

Permalink
Properly initialize AndX respones, guard against NPE when respone not…
Browse files Browse the repository at this point in the history
… set (#321)
  • Loading branch information
mbechler committed Nov 28, 2022
1 parent 3f5d9d7 commit 2684489
Showing 1 changed file with 44 additions and 15 deletions.
59 changes: 44 additions & 15 deletions src/main/java/jcifs/smb/SmbTransportImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -924,20 +924,7 @@ protected void doSend ( Request request ) throws IOException {
@SuppressWarnings ( "unchecked" )
public <T extends CommonServerMessageBlockResponse> T sendrecv ( CommonServerMessageBlockRequest request, T response, Set<RequestParam> params )
throws IOException {
if ( request instanceof jcifs.internal.Request ) {
if ( response == null ) {
response = (T) ( (jcifs.internal.Request<?>) request ).initResponse(getContext());
}
else if ( isSMB2() ) {
throw new IOException("Should not provide response argument for SMB2");
}
}
else {
request.setResponse(response);
}
if ( response == null ) {
throw new IOException("Invalid response");
}
response = setupResponses(request, response);

CommonServerMessageBlockRequest curHead = request;

Expand Down Expand Up @@ -1050,7 +1037,9 @@ else if ( last == null ) {

CommonServerMessageBlockResponse resp = curReq.getResponse();

if ( resp.isReceived() ) {
if ( resp == null ) {
log.warn("Response not properly set up for" + curReq );
} else if ( resp.isReceived() ) {
grantedCredits += resp.getGrantedCredits();
}
CommonServerMessageBlockRequest next = curReq.getNext();
Expand Down Expand Up @@ -1084,6 +1073,46 @@ else if ( !curReq.isResponseAsync() ) {

}

private <T extends CommonServerMessageBlockResponse> T setupResponses(CommonServerMessageBlockRequest request, T response) throws IOException {
if ( request instanceof jcifs.internal.Request ) {
if ( response == null ) {
response = (T) ( (jcifs.internal.Request<?>) request).initResponse(getContext());
}
else if ( isSMB2() ) {
throw new IOException("Should not provide response argument for SMB2");
}
}
else if ( request instanceof AndXServerMessageBlock ) {
AndXServerMessageBlock curReq = (AndXServerMessageBlock) request;
AndXServerMessageBlock curResp = (AndXServerMessageBlock) response;

do {
curReq.setResponse(curResp);

ServerMessageBlock nextReq = curReq.getAndx();
if ( nextReq == null ) {
break;
}
ServerMessageBlock nextResp = curResp.getAndx();
nextReq.setResponse(nextReq);

if ( ! ( nextReq instanceof AndXServerMessageBlock )) {
break;
}
curReq = (AndXServerMessageBlock) nextReq;
curResp = (AndXServerMessageBlock) nextResp;

} while ( true );
}
else {
request.setResponse(response);
}
if ( response == null ) {
throw new IOException("Invalid response");
}
return response;
}


@Override
protected <T extends Response> boolean handleIntermediate ( Request request, T response ) {
Expand Down

0 comments on commit 2684489

Please sign in to comment.