This is an automated email from the ASF dual-hosted git repository.

Alanxtl pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git


The following commit(s) were added to refs/heads/develop by this push:
     new 43348b4b3 fix(getty): check error before using buf in server Write 
(#3434)
43348b4b3 is described below

commit 43348b4b341ef024bb1139b4408380d8e80dd18d
Author: aias00 <[email protected]>
AuthorDate: Thu Jun 18 12:36:32 2026 +0800

    fix(getty): check error before using buf in server Write (#3434)
    
    In RpcServerPackageHandler.Write, both EncodeResponse and EncodeRequest
    were using buf.Len() before checking err. If the encode call returns an
    error, buf could be nil, causing a nil pointer panic. The client-side
    Write already checks err first; this aligns server-side to the same
    pattern.
    
    Co-authored-by: Claude <[email protected]>
---
 remoting/getty/readwriter.go | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/remoting/getty/readwriter.go b/remoting/getty/readwriter.go
index c61561a89..eff19d93a 100644
--- a/remoting/getty/readwriter.go
+++ b/remoting/getty/readwriter.go
@@ -128,30 +128,30 @@ func (p *RpcServerPackageHandler) Write(ss getty.Session, 
pkg any) ([]byte, erro
        maxBufLength := srvConf.GettySessionParam.MaxMsgLen + impl.HEADER_LENGTH
        if ok {
                buf, err := (p.server.codec).EncodeResponse(res)
+               if err != nil {
+                       logger.Warnf("[Remoting][Getty] binary.Write(res=%#v) = 
err=%#v", res, perrors.WithStack(err))
+                       return nil, perrors.WithStack(err)
+               }
                bufLength := buf.Len()
                if bufLength > maxBufLength {
                        logger.Errorf("[Remoting][Getty] data length %d too 
large, max payload=%d", bufLength-impl.HEADER_LENGTH, 
srvConf.GettySessionParam.MaxMsgLen)
                        return nil, perrors.Errorf("Data length %d too large, 
max payload %d", bufLength-impl.HEADER_LENGTH, 
srvConf.GettySessionParam.MaxMsgLen)
                }
-               if err != nil {
-                       logger.Warnf("[Remoting][Getty] binary.Write(res=%#v) = 
err=%#v", res, perrors.WithStack(err))
-                       return nil, perrors.WithStack(err)
-               }
                return buf.Bytes(), nil
        }
 
        req, ok := pkg.(*remoting.Request)
        if ok {
                buf, err := (p.server.codec).EncodeRequest(req)
+               if err != nil {
+                       logger.Warnf("[Remoting][Getty] binary.Write(req=%#v) = 
err=%#v", req, perrors.WithStack(err))
+                       return nil, perrors.WithStack(err)
+               }
                bufLength := buf.Len()
                if bufLength > maxBufLength {
                        logger.Errorf("[Remoting][Getty] data length %d too 
large, max payload=%d", bufLength-impl.HEADER_LENGTH, 
srvConf.GettySessionParam.MaxMsgLen)
                        return nil, perrors.Errorf("Data length %d too large, 
max payload %d", bufLength-impl.HEADER_LENGTH, 
srvConf.GettySessionParam.MaxMsgLen)
                }
-               if err != nil {
-                       logger.Warnf("[Remoting][Getty] binary.Write(req=%#v) = 
err=%#v", req, perrors.WithStack(err))
-                       return nil, perrors.WithStack(err)
-               }
                return buf.Bytes(), nil
        }
 

Reply via email to