Ensure unique image id in single session

This commit is contained in:
Kovid Goyal 2023-03-20 07:55:37 +05:30
parent ef7f13d893
commit 41ea5f0c63
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -278,7 +278,12 @@ func write_unicode_placeholder(imgd *image_data) {
} }
} }
var seen_image_ids *utils.Set[uint32]
func transmit_image(imgd *image_data) { func transmit_image(imgd *image_data) {
if seen_image_ids == nil {
seen_image_ids = utils.NewSet[uint32](32)
}
defer func() { defer func() {
for _, frame := range imgd.frames { for _, frame := range imgd.frames {
if frame.filename_is_temporary && frame.filename != "" { if frame.filename_is_temporary && frame.filename != "" {
@ -315,7 +320,7 @@ func transmit_image(imgd *image_data) {
} }
if imgd.image_id == 0 { if imgd.image_id == 0 {
if imgd.use_unicode_placeholder { if imgd.use_unicode_placeholder {
for imgd.image_id&0xFF000000 == 0 || imgd.image_id&0x00FFFF00 == 0 { for imgd.image_id&0xFF000000 == 0 || imgd.image_id&0x00FFFF00 == 0 || seen_image_ids.Has(imgd.image_id) {
// Generate a 32-bit image id using rejection sampling such that the most // Generate a 32-bit image id using rejection sampling such that the most
// significant byte and the two bytes in the middle are non-zero to avoid // significant byte and the two bytes in the middle are non-zero to avoid
// collisions with applications that cannot represent non-zero most // collisions with applications that cannot represent non-zero most
@ -323,6 +328,7 @@ func transmit_image(imgd *image_data) {
// or two non-zero bytes in the middle (which requires 24-bit color mode). // or two non-zero bytes in the middle (which requires 24-bit color mode).
imgd.image_id = next_random() imgd.image_id = next_random()
} }
seen_image_ids.Add(imgd.image_id)
} else { } else {
if len(imgd.frames) > 1 { if len(imgd.frames) > 1 {
for imgd.image_number == 0 { for imgd.image_number == 0 {