Source file src/encoding/gob/decoder.go

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package gob
     6  
     7  import (
     8  	"bufio"
     9  	"errors"
    10  	"internal/saferio"
    11  	"io"
    12  	"reflect"
    13  	"sync"
    14  )
    15  
    16  // tooBig provides a sanity check for sizes; used in several places. Upper limit
    17  // of is 1GB on 32-bit systems, 8GB on 64-bit, allowing room to grow a little
    18  // without overflow.
    19  const tooBig = (1 << 30) << (^uint(0) >> 62)
    20  
    21  // A Decoder manages the receipt of type and data information read from the
    22  // remote side of a connection.  It is safe for concurrent use by multiple
    23  // goroutines.
    24  //
    25  // The Decoder does only basic sanity checking on decoded input sizes,
    26  // and its limits are not configurable. Take caution when decoding gob data
    27  // from untrusted sources.
    28  type Decoder struct {
    29  	mutex        sync.Mutex                              // each item must be received atomically
    30  	r            io.Reader                               // source of the data
    31  	buf          decBuffer                               // buffer for more efficient i/o from r
    32  	wireType     map[typeId]*wireType                    // map from remote ID to local description
    33  	decoderCache map[reflect.Type]map[typeId]**decEngine // cache of compiled engines
    34  	ignorerCache map[typeId]**decEngine                  // ditto for ignored objects
    35  	freeList     *decoderState                           // list of free decoderStates; avoids reallocation
    36  	countBuf     []byte                                  // used for decoding integers while parsing messages
    37  	err          error
    38  }
    39  
    40  // NewDecoder returns a new decoder that reads from the io.Reader.
    41  // If r does not also implement io.ByteReader, it will be wrapped in a
    42  // bufio.Reader.
    43  func NewDecoder(r io.Reader) *Decoder {
    44  	dec := new(Decoder)
    45  	// We use the ability to read bytes as a plausible surrogate for buffering.
    46  	if _, ok := r.(io.ByteReader); !ok {
    47  		r = bufio.NewReader(r)
    48  	}
    49  	dec.r = r
    50  	dec.wireType = make(map[typeId]*wireType)
    51  	dec.decoderCache = make(map[reflect.Type]map[typeId]**decEngine)
    52  	dec.ignorerCache = make(map[typeId]**decEngine)
    53  	dec.countBuf = make([]byte, 9) // counts may be uint64s (unlikely!), require 9 bytes
    54  
    55  	return dec
    56  }
    57  
    58  // recvType loads the definition of a type.
    59  func (dec *Decoder) recvType(id typeId) {
    60  	// Have we already seen this type? That's an error
    61  	if id < firstUserId || dec.wireType[id] != nil {
    62  		dec.err = errors.New("gob: duplicate type received")
    63  		return
    64  	}
    65  
    66  	// Type:
    67  	wire := new(wireType)
    68  	dec.decodeValue(tWireType, reflect.ValueOf(wire))
    69  	if dec.err != nil {
    70  		return
    71  	}
    72  	// Remember we've seen this type.
    73  	dec.wireType[id] = wire
    74  }
    75  
    76  var errBadCount = errors.New("invalid message length")
    77  
    78  // recvMessage reads the next count-delimited item from the input. It is the converse
    79  // of Encoder.writeMessage. It returns false on EOF or other error reading the message.
    80  func (dec *Decoder) recvMessage() bool {
    81  	// Read a count.
    82  	nbytes, _, err := decodeUintReader(dec.r, dec.countBuf)
    83  	if err != nil {
    84  		dec.err = err
    85  		return false
    86  	}
    87  	if nbytes >= tooBig {
    88  		dec.err = errBadCount
    89  		return false
    90  	}
    91  	dec.readMessage(int(nbytes))
    92  	return dec.err == nil
    93  }
    94  
    95  // readMessage reads the next nbytes bytes from the input.
    96  func (dec *Decoder) readMessage(nbytes int) {
    97  	if dec.buf.Len() != 0 {
    98  		// The buffer should always be empty now.
    99  		panic("non-empty decoder buffer")
   100  	}
   101  	// Read the data
   102  	var buf []byte
   103  	buf, dec.err = saferio.ReadData(dec.r, uint64(nbytes))
   104  	dec.buf.SetBytes(buf)
   105  	if dec.err == io.EOF {
   106  		dec.err = io.ErrUnexpectedEOF
   107  	}
   108  }
   109  
   110  // toInt turns an encoded uint64 into an int, according to the marshaling rules.
   111  func toInt(x uint64) int64 {
   112  	i := int64(x >> 1)
   113  	if x&1 != 0 {
   114  		i = ^i
   115  	}
   116  	return i
   117  }
   118  
   119  func (dec *Decoder) nextInt() int64 {
   120  	n, _, err := decodeUintReader(&dec.buf, dec.countBuf)
   121  	if err != nil {
   122  		dec.err = err
   123  	}
   124  	return toInt(n)
   125  }
   126  
   127  func (dec *Decoder) nextUint() uint64 {
   128  	n, _, err := decodeUintReader(&dec.buf, dec.countBuf)
   129  	if err != nil {
   130  		dec.err = err
   131  	}
   132  	return n
   133  }
   134  
   135  // decodeTypeSequence parses:
   136  // TypeSequence
   137  //
   138  //	(TypeDefinition DelimitedTypeDefinition*)?
   139  //
   140  // and returns the type id of the next value. It returns -1 at
   141  // EOF.  Upon return, the remainder of dec.buf is the value to be
   142  // decoded. If this is an interface value, it can be ignored by
   143  // resetting that buffer.
   144  func (dec *Decoder) decodeTypeSequence(isInterface bool) typeId {
   145  	firstMessage := true
   146  	for dec.err == nil {
   147  		if dec.buf.Len() == 0 {
   148  			if !dec.recvMessage() {
   149  				// We can only return io.EOF if the input was empty.
   150  				// If we read one or more type spec messages,
   151  				// require a data item message to follow.
   152  				// If we hit an EOF before that, then give ErrUnexpectedEOF.
   153  				if !firstMessage && dec.err == io.EOF {
   154  					dec.err = io.ErrUnexpectedEOF
   155  				}
   156  				break
   157  			}
   158  		}
   159  		// Receive a type id.
   160  		id := typeId(dec.nextInt())
   161  		if id >= 0 {
   162  			// Value follows.
   163  			return id
   164  		}
   165  		// Type definition for (-id) follows.
   166  		dec.recvType(-id)
   167  		if dec.err != nil {
   168  			break
   169  		}
   170  		// When decoding an interface, after a type there may be a
   171  		// DelimitedValue still in the buffer. Skip its count.
   172  		// (Alternatively, the buffer is empty and the byte count
   173  		// will be absorbed by recvMessage.)
   174  		if dec.buf.Len() > 0 {
   175  			if !isInterface {
   176  				dec.err = errors.New("extra data in buffer")
   177  				break
   178  			}
   179  			dec.nextUint()
   180  		}
   181  		firstMessage = false
   182  	}
   183  	return -1
   184  }
   185  
   186  // Decode reads the next value from the input stream and stores
   187  // it in the data represented by the empty interface value.
   188  // If e is nil, the value will be discarded. Otherwise,
   189  // the value underlying e must be a pointer to the
   190  // correct type for the next data item received.
   191  // If the input is at EOF, Decode returns io.EOF and
   192  // does not modify e.
   193  func (dec *Decoder) Decode(e any) error {
   194  	if e == nil {
   195  		return dec.DecodeValue(reflect.Value{})
   196  	}
   197  	value := reflect.ValueOf(e)
   198  	// If e represents a value as opposed to a pointer, the answer won't
   199  	// get back to the caller. Make sure it's a pointer.
   200  	if value.Type().Kind() != reflect.Pointer {
   201  		dec.err = errors.New("gob: attempt to decode into a non-pointer")
   202  		return dec.err
   203  	}
   204  	return dec.DecodeValue(value)
   205  }
   206  
   207  // DecodeValue reads the next value from the input stream.
   208  // If v is the zero reflect.Value (v.Kind() == Invalid), DecodeValue discards the value.
   209  // Otherwise, it stores the value into v. In that case, v must represent
   210  // a non-nil pointer to data or be an assignable reflect.Value (v.CanSet())
   211  // If the input is at EOF, DecodeValue returns io.EOF and
   212  // does not modify v.
   213  func (dec *Decoder) DecodeValue(v reflect.Value) error {
   214  	if v.IsValid() {
   215  		if v.Kind() == reflect.Pointer && !v.IsNil() {
   216  			// That's okay, we'll store through the pointer.
   217  		} else if !v.CanSet() {
   218  			return errors.New("gob: DecodeValue of unassignable value")
   219  		}
   220  	}
   221  	// Make sure we're single-threaded through here.
   222  	dec.mutex.Lock()
   223  	defer dec.mutex.Unlock()
   224  
   225  	dec.buf.Reset() // In case data lingers from previous invocation.
   226  	dec.err = nil
   227  	id := dec.decodeTypeSequence(false)
   228  	if dec.err == nil {
   229  		dec.decodeValue(id, v)
   230  	}
   231  	return dec.err
   232  }
   233  
   234  // If debug.go is compiled into the program, debugFunc prints a human-readable
   235  // representation of the gob data read from r by calling that file's Debug function.
   236  // Otherwise it is nil.
   237  var debugFunc func(io.Reader)
   238  

View as plain text