mirror of
				https://github.com/yggdrasil-network/yggdrasil-go.git
				synced 2025-11-04 11:15:07 +03:00 
			
		
		
		
	properly fix the memory errors, it was caused by a function returning and PutBytes-ing a buffer before a worker had a chance to decrypt the buffer, so it would GetBytes the same buffer by dumb luck and then get an illegal overlap
This commit is contained in:
		
							parent
							
								
									01ea6d3d80
								
							
						
					
					
						commit
						5d323861f0
					
				
					 2 changed files with 4 additions and 13 deletions
				
			
		| 
						 | 
					@ -199,7 +199,7 @@ func (c *Conn) Read(b []byte) (int, error) {
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			// Decrypt the packet
 | 
								// Decrypt the packet
 | 
				
			||||||
			bs, isOK := crypto.BoxOpen(&sinfo.sharedSesKey, p.Payload, &p.Nonce)
 | 
								bs, isOK := crypto.BoxOpen(&sinfo.sharedSesKey, p.Payload, &p.Nonce)
 | 
				
			||||||
			defer util.PutBytes(bs)
 | 
								defer util.PutBytes(bs) // FIXME commenting this out leads to illegal buffer reuse, this implies there's a memory error somewhere and that this is just flooding things out of the finite pool of old slices that get reused
 | 
				
			||||||
			// Check if we were unable to decrypt the packet for some reason and
 | 
								// Check if we were unable to decrypt the packet for some reason and
 | 
				
			||||||
			// return an error if we couldn't
 | 
								// return an error if we couldn't
 | 
				
			||||||
			if !isOK {
 | 
								if !isOK {
 | 
				
			||||||
| 
						 | 
					@ -223,11 +223,7 @@ func (c *Conn) Read(b []byte) (int, error) {
 | 
				
			||||||
		case <-timer.C:
 | 
							case <-timer.C:
 | 
				
			||||||
			return 0, ConnError{errors.New("Timeout"), true, false}
 | 
								return 0, ConnError{errors.New("Timeout"), true, false}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		select { // Wait for worker to return
 | 
							<-done // Wait for the worker to finish, failing this can cause memory errors (util.[Get||Put]Bytes stuff)
 | 
				
			||||||
		case <-done:
 | 
					 | 
				
			||||||
		case <-timer.C:
 | 
					 | 
				
			||||||
			return 0, ConnError{errors.New("Timeout"), true, false}
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		// Something went wrong in the session worker so abort
 | 
							// Something went wrong in the session worker so abort
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return 0, err
 | 
								return 0, err
 | 
				
			||||||
| 
						 | 
					@ -270,7 +266,6 @@ func (c *Conn) Write(b []byte) (bytesWritten int, err error) {
 | 
				
			||||||
			return 0, errors.New("search failed")
 | 
								return 0, errors.New("search failed")
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	// defer util.PutBytes(b)
 | 
					 | 
				
			||||||
	var packet []byte
 | 
						var packet []byte
 | 
				
			||||||
	done := make(chan struct{})
 | 
						done := make(chan struct{})
 | 
				
			||||||
	workerFunc := func() {
 | 
						workerFunc := func() {
 | 
				
			||||||
| 
						 | 
					@ -294,11 +289,7 @@ func (c *Conn) Write(b []byte) (bytesWritten int, err error) {
 | 
				
			||||||
	case <-timer.C:
 | 
						case <-timer.C:
 | 
				
			||||||
		return 0, ConnError{errors.New("Timeout"), true, false}
 | 
							return 0, ConnError{errors.New("Timeout"), true, false}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	select { // Wait for worker to return
 | 
						<-done // Wait for the worker to finish, failing this can cause memory errors (util.[Get||Put]Bytes stuff)
 | 
				
			||||||
	case <-done:
 | 
					 | 
				
			||||||
	case <-timer.C:
 | 
					 | 
				
			||||||
		return 0, ConnError{errors.New("Timeout"), true, false}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	// Give the packet to the router
 | 
						// Give the packet to the router
 | 
				
			||||||
	sinfo.core.router.out(packet)
 | 
						sinfo.core.router.out(packet)
 | 
				
			||||||
	// Finally return the number of bytes we wrote
 | 
						// Finally return the number of bytes we wrote
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -341,7 +341,7 @@ func (r *router) handleTraffic(packet []byte) {
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	select {
 | 
						select {
 | 
				
			||||||
	case sinfo.recv <- &p: // FIXME ideally this should be FIFO
 | 
						case sinfo.recv <- &p: // FIXME ideally this should be front drop
 | 
				
			||||||
	default:
 | 
						default:
 | 
				
			||||||
		util.PutBytes(p.Payload)
 | 
							util.PutBytes(p.Payload)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue