From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <yew-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
	by lore.proxmox.com (Postfix) with ESMTPS id 8EA4D1FF164
	for <inbox@lore.proxmox.com>; Fri,  9 May 2025 09:31:20 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 93F2937459;
	Fri,  9 May 2025 09:31:39 +0200 (CEST)
From: Dominik Csapak <d.csapak@proxmox.com>
To: yew-devel@lists.proxmox.com
Date: Fri,  9 May 2025 09:31:03 +0200
Message-Id: <20250509073103.428000-1-d.csapak@proxmox.com>
X-Mailer: git-send-email 2.39.5
MIME-Version: 1.0
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.022 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to
 Validity was blocked. See
 https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more
 information.
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [proxmox.com]
Subject: [yew-devel] [PATCH yew-widget-toolkit v2] widget: data table:
 prevent duplicate yew keys for rows
X-BeenThere: yew-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Yew framework devel list at Proxmox <yew-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/yew-devel>, 
 <mailto:yew-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/yew-devel/>
List-Post: <mailto:yew-devel@lists.proxmox.com>
List-Help: <mailto:yew-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/yew-devel>, 
 <mailto:yew-devel-request@lists.proxmox.com?subject=subscribe>
Reply-To: Yew framework devel list at Proxmox <yew-devel@lists.proxmox.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: yew-devel-bounces@lists.proxmox.com
Sender: "yew-devel" <yew-devel-bounces@lists.proxmox.com>

Yews diff algorithm assumes that elements in a list have unique keys, so
it can properly diff the new and old list, but our Store/Datastore does
not guarantee that records have unique keys.

For the data table rows, we used the record keys as yew keys, so this is
a problem when keys in the store are duplicated.

To prevent an application wide panic, modify keys for DataTableRows that
are to be displayed and have duplicated keys.

In debug mode, log a prominent warning, so it's easier to detect.

This is more in depth prevention of crashes/problem like in bug #6382.

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
changes from v1:
* rewrite commit message a bit to be clearer.
* remove the 'fix' tag in the commit subject. Though it would fix the
  immediate crash, the proper fix is [0] and this is just another layer
  of defense against such errors

0: https://lore.proxmox.com/yew-devel/20250509071627.331064-1-d.csapak@proxmox.com/T/#u

 src/widget/data_table/data_table.rs | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/src/widget/data_table/data_table.rs b/src/widget/data_table/data_table.rs
index a8fda7b..ab49d0d 100644
--- a/src/widget/data_table/data_table.rs
+++ b/src/widget/data_table/data_table.rs
@@ -919,14 +919,26 @@ impl<S: DataStore> PwtDataTable<S> {
             }
         }
 
+        // keep track of keys that are displayed, to avoid duplicates
+        let mut key_set = HashSet::new();
+
         for (filtered_pos, item) in props.store.filtered_data_range(start..end) {
-            let record_key = props.store.extract_key(&*item.record());
+            let mut record_key = props.store.extract_key(&*item.record());
 
             let mut selected = false;
             if let Some(selection) = &props.selection {
                 selected = selection.contains(&record_key);
             }
 
+            if key_set.contains(&record_key) {
+                #[cfg(debug_assertions)]
+                log::error!("duplicate key found: {record_key}");
+                // avoid duplicate key by adding the row num to the key
+                record_key = Key::from(format!("{filtered_pos}-{record_key}"))
+            }
+
+            key_set.insert(record_key.clone());
+
             let active = cursor
                 .map(|cursor| cursor == filtered_pos)
                 // if no cursor, mark first row active
-- 
2.39.5



_______________________________________________
yew-devel mailing list
yew-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/yew-devel