Merge pull request #249 from jack15083/master

fix not enough buf error when decode date
diff --git a/.github/workflows/github-actions.yml b/.github/workflows/github-actions.yml
index 50d76a1..c74669a 100644
--- a/.github/workflows/github-actions.yml
+++ b/.github/workflows/github-actions.yml
@@ -73,7 +73,7 @@
       run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.27.0
 
     - name: Run Linter
-      run: golangci-lint run --timeout=10m -v --disable-all --enable=govet --enable=staticcheck --enable=ineffassign --enable=misspell
+      run: golangci-lint run --timeout=10m -v
 
     - name: Go Test
       run: GO111MODULE=on && go mod vendor && go test -race -v && go test -bench . -race -coverprofile=coverage.txt
diff --git a/.golangci.yml b/.golangci.yml
index ec838c6..867c6d4 100644
--- a/.golangci.yml
+++ b/.golangci.yml
@@ -33,11 +33,12 @@
       - wrapperFunc
 
 linters:
-  enable-all: true
-  disable:
-    - maligned
-    - prealloc
-    - gochecknoglobals
+  disable-all: true
+  enable:
+    - govet
+    - staticcheck
+    - ineffassign
+    - misspell
 
 run:
   skip-dirs:
diff --git a/date.go b/date.go
index 5fe83d4..2dae3b3 100644
--- a/date.go
+++ b/date.go
@@ -40,8 +40,7 @@
 	value := UnpackPtrValue(reflect.ValueOf(i))
 	vi := value.Interface().(time.Time)
 	if vi == ZeroDate {
-		b = append(b, BC_NULL)
-		return nil
+		return append(b, BC_NULL)
 	}
 	b = append(b, BC_DATE)
 	return append(b, PackInt64(vi.UnixNano()/1e6)...)
diff --git a/go.mod b/go.mod
index fb0a269..3ee2f1e 100644
--- a/go.mod
+++ b/go.mod
@@ -5,3 +5,4 @@
 	github.com/pkg/errors v0.9.1
 	github.com/stretchr/testify v1.4.0
 )
+
diff --git a/hessian.go b/hessian.go
index d4fb135..cd9ab70 100644
--- a/hessian.go
+++ b/hessian.go
@@ -98,10 +98,8 @@
 		return packResponse(header, body)
 
 	default:
-		return nil, perrors.Errorf("Unrecognised message type: %v", header.Type)
+		return nil, perrors.Errorf("Unrecognized message type: %v", header.Type)
 	}
-
-	// unreachable return nil, nil
 }
 
 // ReadHeader uses hessian codec to read dubbo header
@@ -189,9 +187,9 @@
 	switch h.pkgType & PackageType_BitSize {
 	case PackageResponse | PackageHeartbeat | PackageResponse_Exception, PackageResponse | PackageResponse_Exception:
 		decoder := NewDecoder(buf[:])
-		exception, err := decoder.Decode()
-		if err != nil {
-			return perrors.WithStack(err)
+		exception, decErr := decoder.Decode()
+		if decErr != nil {
+			return perrors.WithStack(decErr)
 		}
 		rsp, ok := rspObj.(*Response)
 		if !ok {
diff --git a/hessian_test.go b/hessian_test.go
index b712838..90cd4c4 100644
--- a/hessian_test.go
+++ b/hessian_test.go
@@ -79,6 +79,7 @@
 
 func doTestResponse(t *testing.T, packageType PackageType, responseStatus byte, body interface{}, decodedResponse *Response, assertFunc func()) {
 	resp, err := doTestHessianEncodeHeader(t, packageType, responseStatus, body)
+	assert.Nil(t, err)
 
 	codecR := NewHessianCodec(bufio.NewReader(bytes.NewReader(resp)))
 
@@ -162,6 +163,7 @@
 
 func doTestRequest(t *testing.T, packageType PackageType, responseStatus byte, body interface{}) {
 	resp, err := doTestHessianEncodeHeader(t, packageType, responseStatus, body)
+	assert.Nil(t, err)
 
 	codecR := NewHessianCodec(bufio.NewReader(bytes.NewReader(resp)))
 
diff --git a/list.go b/list.go
index 8c70310..938d567 100644
--- a/list.go
+++ b/list.go
@@ -305,7 +305,7 @@
 
 	isVariableArr := tag == BC_LIST_VARIABLE
 
-	length := -1
+	var length int
 	if listFixedTypedLenTag(tag) {
 		length = int(tag - _listFixedTypedLenTagMin)
 	} else if tag == BC_LIST_FIXED {
diff --git a/long.go b/long.go
index e170700..8a6bdc2 100644
--- a/long.go
+++ b/long.go
@@ -113,8 +113,8 @@
 		return int64(int(buf[0])<<8 + int(buf[1])), nil
 
 	case tag == BC_INT: // 'I'
-		i32, err := d.decInt32(TAG_READ)
-		return int64(i32), err
+		i32, decErr := d.decInt32(TAG_READ)
+		return int64(i32), decErr
 
 	case tag == BC_LONG_INT:
 		var i32 int32
diff --git a/map.go b/map.go
index c6e3044..2b26d30 100644
--- a/map.go
+++ b/map.go
@@ -170,9 +170,9 @@
 		// null map tag check
 		return nil
 	case BC_REF:
-		refObj, err := d.decRef(int32(tag))
-		if err != nil {
-			return perrors.WithStack(err)
+		refObj, decErr := d.decRef(int32(tag))
+		if decErr != nil {
+			return perrors.WithStack(decErr)
 		}
 		SetValue(value, EnsurePackValue(refObj))
 		return nil
diff --git a/object.go b/object.go
index 7880d8b..451cd54 100644
--- a/object.go
+++ b/object.go
@@ -99,7 +99,6 @@
 //x51 x91                   # object ref #1, i.e. Color.GREEN
 func (e *Encoder) encObject(v interface{}) error {
 	var (
-		ok     bool
 		i      int
 		idx    int
 		num    int
@@ -141,6 +140,7 @@
 		}
 	}
 
+	var ok bool
 	if idx == -1 {
 		idx, ok = checkPOJORegistry(typeof(v))
 		if !ok {
@@ -425,9 +425,9 @@
 				// java enum
 				if fldRawValue.Type().Implements(javaEnumType) {
 					d.unreadByte() // Enum parsing, decInt64 above has read a byte, so you need to return a byte here
-					s, err := d.DecodeValue()
-					if err != nil {
-						return nil, perrors.Wrapf(err, "decInstance->decObject field name:%s", fieldName)
+					s, decErr := d.DecodeValue()
+					if decErr != nil {
+						return nil, perrors.Wrapf(decErr, "decInstance->decObject field name:%s", fieldName)
 					}
 					enumValue, _ := s.(JavaEnum)
 					num = int32(enumValue)
@@ -447,9 +447,9 @@
 			if err != nil {
 				if fldTyp.Implements(javaEnumType) {
 					d.unreadByte() // Enum parsing, decInt64 above has read a byte, so you need to return a byte here
-					s, err := d.Decode()
-					if err != nil {
-						return nil, perrors.Wrapf(err, "decInstance->decObject field name:%s", fieldName)
+					s, decErr := d.Decode()
+					if decErr != nil {
+						return nil, perrors.Wrapf(decErr, "decInstance->decObject field name:%s", fieldName)
 					}
 					enumValue, _ := s.(JavaEnum)
 					num = int64(enumValue)
@@ -516,8 +516,8 @@
 				err error
 				s   interface{}
 			)
-			typ := UnpackPtrType(fldRawValue.Type())
-			if typ.String() == "time.Time" {
+			fldType := UnpackPtrType(fldRawValue.Type())
+			if fldType.String() == "time.Time" {
 				s, err = d.decDate(TAG_READ)
 				if err != nil {
 					return nil, perrors.WithStack(err)
@@ -641,9 +641,9 @@
 	case tag == BC_REF:
 		return d.decRef(int32(tag))
 	case tag == BC_OBJECT_DEF:
-		clsDef, err := d.decClassDef()
-		if err != nil {
-			return nil, perrors.Wrap(err, "decObject->decClassDef byte double")
+		clsDef, decErr := d.decClassDef()
+		if decErr != nil {
+			return nil, perrors.Wrap(decErr, "decObject->decClassDef byte double")
 		}
 		cls, _ = clsDef.(*classInfo)
 		//add to slice
diff --git a/pojo.go b/pojo.go
index 272037e..b27a3c6 100644
--- a/pojo.go
+++ b/pojo.go
@@ -135,23 +135,23 @@
 	}
 
 	var (
-		bHeader    []byte
-		bBody      []byte
-		fieldList  []string
-		structInfo structInfo
-		clsDef     classInfo
+		bHeader   []byte
+		bBody     []byte
+		fieldList []string
+		sttInfo   structInfo
+		clsDef    classInfo
 	)
 
-	structInfo.typ = obtainValueType(o)
+	sttInfo.typ = obtainValueType(o)
 
-	structInfo.goName = structInfo.typ.String()
-	structInfo.javaName = javaClassName
-	structInfo.inst = o
-	pojoRegistry.j2g[structInfo.javaName] = structInfo.goName
-	registerTypeName(structInfo.goName, structInfo.javaName)
+	sttInfo.goName = sttInfo.typ.String()
+	sttInfo.javaName = javaClassName
+	sttInfo.inst = o
+	pojoRegistry.j2g[sttInfo.javaName] = sttInfo.goName
+	registerTypeName(sttInfo.goName, sttInfo.javaName)
 
 	// prepare fields info of objectDef
-	nextStruct := []reflect.Type{structInfo.typ}
+	nextStruct := []reflect.Type{sttInfo.typ}
 	for len(nextStruct) > 0 {
 		current := nextStruct[0]
 		if current.Kind() == reflect.Struct {
@@ -192,23 +192,23 @@
 
 	// prepare header of objectDef
 	bHeader = encByte(bHeader, BC_OBJECT_DEF)
-	bHeader = encString(bHeader, structInfo.javaName)
+	bHeader = encString(bHeader, sttInfo.javaName)
 
 	// write fields length into header of objectDef
 	// note: cause fieldList is a dynamic slice, so one must calculate length only after it being prepared already.
 	bHeader = encInt32(bHeader, int32(len(fieldList)))
 
 	// prepare classDef
-	clsDef = classInfo{javaName: structInfo.javaName, fieldNameList: fieldList}
+	clsDef = classInfo{javaName: sttInfo.javaName, fieldNameList: fieldList}
 
 	// merge header and body of objectDef into buffer of classInfo
 	clsDef.buffer = append(bHeader, bBody...)
 
-	structInfo.index = len(pojoRegistry.classInfoList)
+	sttInfo.index = len(pojoRegistry.classInfoList)
 	pojoRegistry.classInfoList = append(pojoRegistry.classInfoList, &clsDef)
-	pojoRegistry.registry[structInfo.goName] = &structInfo
+	pojoRegistry.registry[sttInfo.goName] = &sttInfo
 
-	return structInfo.index
+	return sttInfo.index
 }
 
 // UnRegisterPOJOs unregister POJO instances. It is easy for test.
diff --git a/string.go b/string.go
index 2581585..987a6ac 100644
--- a/string.go
+++ b/string.go
@@ -152,19 +152,20 @@
 	}
 
 	var (
-		byteLen = 0
-		charLen = 0
+		byteLen int
+		charLen int
 		vBuf    = *bytes.NewBufferString(v)
 
-		byteRead  = 0
-		charCount = 0
-		byteCount = 0
+		byteRead  int
+		charCount int
+		byteCount int
 	)
 
 	bufp := gxbytes.AcquireBytes(CHUNK_SIZE * 3)
 	defer gxbytes.ReleaseBytes(bufp)
 	buf := *bufp
 
+	byteRead = 0
 	for {
 		if vBuf.Len() <= 0 {
 			break
@@ -433,6 +434,9 @@
 								} else {
 									// out of the chunk byte data
 									bytesBuf[i+4], err = d.reader.ReadByte()
+									if err != nil {
+										return s, perrors.WithStack(err)
+									}
 									ch1 = bytesBuf[i+4]
 									nread++
 									len++
@@ -449,6 +453,9 @@
 										return s, perrors.WithStack(err)
 									}
 									ch2, err = d.reader.ReadByte()
+									if err != nil {
+										return s, perrors.WithStack(err)
+									}
 									len += 2
 									nread += 2
 								}