Files
mev-beta/pkg/transport/utils.go
Krypto Kajun 850223a953 fix(multicall): resolve critical multicall parsing corruption issues
- Added comprehensive bounds checking to prevent buffer overruns in multicall parsing
- Implemented graduated validation system (Strict/Moderate/Permissive) to reduce false positives
- Added LRU caching system for address validation with 10-minute TTL
- Enhanced ABI decoder with missing Universal Router and Arbitrum-specific DEX signatures
- Fixed duplicate function declarations and import conflicts across multiple files
- Added error recovery mechanisms with multiple fallback strategies
- Updated tests to handle new validation behavior for suspicious addresses
- Fixed parser test expectations for improved validation system
- Applied gofmt formatting fixes to ensure code style compliance
- Fixed mutex copying issues in monitoring package by introducing MetricsSnapshot
- Resolved critical security vulnerabilities in heuristic address extraction
- Progress: Updated TODO audit from 10% to 35% complete

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-17 00:12:55 -05:00

49 lines
1.2 KiB
Go

package transport
import (
"encoding/json"
"fmt"
)
// ExtractMessage extracts a message from a byte buffer with length prefix format
// Format: "length\nmessage_data"
func ExtractMessage(buffer []byte) (*Message, []byte, error) {
// Look for length prefix (format: "length\nmessage_data")
newlineIndex := -1
for i, b := range buffer {
if b == '\n' {
newlineIndex = i
break
}
}
if newlineIndex == -1 {
return nil, buffer, nil // No complete length prefix yet
}
// Parse length
lengthStr := string(buffer[:newlineIndex])
var messageLength int
if _, err := fmt.Sscanf(lengthStr, "%d", &messageLength); err != nil {
return nil, nil, fmt.Errorf("invalid length prefix: %s", lengthStr)
}
// Check if we have the complete message
messageStart := newlineIndex + 1
messageEnd := messageStart + messageLength
if len(buffer) < messageEnd {
return nil, buffer, nil // Incomplete message
}
// Extract and parse message
messageData := buffer[messageStart:messageEnd]
var msg Message
if err := json.Unmarshal(messageData, &msg); err != nil {
return nil, nil, fmt.Errorf("failed to unmarshal message: %w", err)
}
// Return message and remaining buffer
remaining := buffer[messageEnd:]
return &msg, remaining, nil
}