Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix code scanning alert no. 50: Size computation for allocation may overflow #1088

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pkg/blockcontroller/blockcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,10 @@ func (bc *BlockController) DoRunShellCommand(rc *RunShellOpts, blockMeta waveobj
go func() {
// handles outputCh -> shellInputCh
for msg := range wshProxy.ToRemoteCh {
encodedMsg := wshutil.EncodeWaveOSCBytes(wshutil.WaveServerOSC, msg)
encodedMsg, err := wshutil.EncodeWaveOSCBytes(wshutil.WaveServerOSC, msg)
if err != nil {
log.Printf("error encoding OSC message: %v\n", err)
}
shellInputCh <- &BlockInputUnion{InputData: encodedMsg}
}
}()
Expand Down
8 changes: 5 additions & 3 deletions pkg/wshutil/wshrpcio.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,12 @@ func AdaptMsgChToPty(outputCh chan []byte, oscEsc string, output io.Writer) erro
panic("oscEsc must be 5 characters")
}
for msg := range outputCh {
barr := EncodeWaveOSCBytes(oscEsc, msg)
_, err := output.Write(barr)
barr, err := EncodeWaveOSCBytes(oscEsc, msg)
if err != nil {
return fmt.Errorf("error writing to output: %w", err)
return fmt.Errorf("error encoding osc message (AdaptMsgChToPty): %w", err)
}
if _, err := output.Write(barr); err != nil {
return fmt.Errorf("error writing osc message (AdaptMsgChToPty): %w", err)
}
}
return nil
Expand Down
20 changes: 14 additions & 6 deletions pkg/wshutil/wshutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,13 @@ func makeOscPrefix(oscNum string) []byte {
return output
}

func EncodeWaveOSCBytes(oscNum string, barr []byte) []byte {
func EncodeWaveOSCBytes(oscNum string, barr []byte) ([]byte, error) {
if len(oscNum) != 5 {
panic("oscNum must be 5 characters")
return nil, fmt.Errorf("oscNum must be 5 characters")
}
const maxSize = 64 * 1024 * 1024 // 64 MB
if len(barr) > maxSize {
return nil, fmt.Errorf("input data too large")
}
hasControlChars := false
for _, b := range barr {
Expand All @@ -85,7 +89,7 @@ func EncodeWaveOSCBytes(oscNum string, barr []byte) []byte {
copyOscPrefix(output, oscNum)
copy(output[oscPrefixLen(oscNum):], barr)
output[len(output)-1] = BEL
return output
return output, nil
}

var buf bytes.Buffer
Expand All @@ -101,7 +105,7 @@ func EncodeWaveOSCBytes(oscNum string, barr []byte) []byte {
}
}
buf.WriteByte(BEL)
return buf.Bytes()
return buf.Bytes(), nil
}

func EncodeWaveOSCMessageEx(oscNum string, msg *RpcMessage) ([]byte, error) {
Expand All @@ -112,7 +116,7 @@ func EncodeWaveOSCMessageEx(oscNum string, msg *RpcMessage) ([]byte, error) {
if err != nil {
return nil, fmt.Errorf("error marshalling message to json: %w", err)
}
return EncodeWaveOSCBytes(oscNum, barr), nil
return EncodeWaveOSCBytes(oscNum, barr)
}

var termModeLock = sync.Mutex{}
Expand Down Expand Up @@ -194,7 +198,11 @@ func SetupTerminalRpcClient(serverImpl ServerImpl) (*WshRpc, io.Reader) {
rpcClient := MakeWshRpc(messageCh, outputCh, wshrpc.RpcContext{}, serverImpl)
go func() {
for msg := range outputCh {
barr := EncodeWaveOSCBytes(WaveOSC, msg)
barr, err := EncodeWaveOSCBytes(WaveOSC, msg)
if err != nil {
fmt.Fprintf(os.Stderr, "Error encoding OSC message: %v\n", err)
continue
}
os.Stdout.Write(barr)
}
}()
Expand Down
Loading