Skip to content
Closed
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
10 changes: 10 additions & 0 deletions wmiau.go
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,7 @@ func (mycli *MyClient) myEventHandler(rawEvt interface{}) {
// try to get Image if any
img := evt.Message.GetImageMessage()
if img != nil {
func() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

While wrapping this block in a closure prevents the main event handler from aborting on error, the implementation inside (specifically line 860) is susceptible to a panic if mime.ExtensionsByType returns an empty slice (which happens for unknown or empty MIME types). Additionally, any early return within this closure (e.g., at line 901) will bypass the os.Remove(tmpPath) call at line 914, leading to temporary file leaks in /tmp. It is recommended to use defer os.Remove(tmpPath) once the path is determined to ensure cleanup regardless of how the closure exits.

// Create a temporary directory in /tmp
tmpDirectory := filepath.Join("/tmp", "user_"+txtid)
errDir := os.MkdirAll(tmpDirectory, 0751)
Expand Down Expand Up @@ -916,11 +917,13 @@ func (mycli *MyClient) myEventHandler(rawEvt interface{}) {
} else {
log.Info().Str("path", tmpPath).Msg("Temporary file deleted")
}
}()
}

// try to get Audio if any
audio := evt.Message.GetAudioMessage()
if audio != nil {
func() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to the image block, early returns within this closure (e.g., at line 991) will skip the file cleanup at line 1004. Using defer os.Remove(tmpPath) would prevent potential file leaks.

// Create a temporary directory in /tmp
tmpDirectory := filepath.Join("/tmp", "user_"+txtid)
errDir := os.MkdirAll(tmpDirectory, 0751)
Expand Down Expand Up @@ -1004,11 +1007,13 @@ func (mycli *MyClient) myEventHandler(rawEvt interface{}) {
} else {
log.Info().Str("path", tmpPath).Msg("Temporary file deleted")
}
}()
}

// try to get Document if any
document := evt.Message.GetDocumentMessage()
if document != nil {
func() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Early returns within this closure (e.g., at line 1086) will skip the file cleanup at line 1099. Consider using defer for more reliable resource management.

// Create a temporary directory in /tmp
tmpDirectory := filepath.Join("/tmp", "user_"+txtid)
errDir := os.MkdirAll(tmpDirectory, 0751)
Expand Down Expand Up @@ -1097,11 +1102,13 @@ func (mycli *MyClient) myEventHandler(rawEvt interface{}) {
} else {
log.Info().Str("path", tmpPath).Msg("Temporary file deleted")
}
}()
}

// try to get Video if any
video := evt.Message.GetVideoMessage()
if video != nil {
func() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This block is also susceptible to a panic at line 1129 if exts is empty. Furthermore, early returns will bypass the os.Remove call at line 1183, causing file leaks. Implementing a fallback extension (similar to the audio block's logic) and using defer for cleanup is advised.

// Create a temporary directory in /tmp
tmpDirectory := filepath.Join("/tmp", "user_"+txtid)
errDir := os.MkdirAll(tmpDirectory, 0751)
Expand Down Expand Up @@ -1179,10 +1186,12 @@ func (mycli *MyClient) myEventHandler(rawEvt interface{}) {
} else {
log.Info().Str("path", tmpPath).Msg("Temporary file deleted")
}
}()
}

sticker := evt.Message.GetStickerMessage()
if sticker != nil {
func() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Early returns within this closure (e.g., at line 1251) will skip the file cleanup at line 1262. Using defer os.Remove(tmpPath) is recommended to ensure temporary files are always deleted.

tmpDirectory := filepath.Join("/tmp", "user_"+txtid)
errDir := os.MkdirAll(tmpDirectory, 0751)
if errDir != nil {
Expand Down Expand Up @@ -1253,6 +1262,7 @@ func (mycli *MyClient) myEventHandler(rawEvt interface{}) {
if err := os.Remove(tmpPath); err != nil {
log.Error().Err(err).Msg("Failed to delete temporary file")
}
}()
}

}
Expand Down