Repository: activeloopai/deeplake

Issue:
[BUG] Possibly unsafe conversion of DatasetDiff instance to bytes
## 🐛🐛 Bug Report

### ⚗️ Current Behavior

The code block

https://github.com/activeloopai/deeplake/blob/c88925d8afa94841972fe4fd6f1ddeb1ede8dde0/deeplake/core/version_control/dataset_diff.py#L37-L39

assumes `len(str.encode('utf-8')) == len(str)` which is not true in general.

### 🧰 Possible Solution

The code block should read:
```python
                            len(old_name_encoded).to_bytes(8, "big"),
                            len(new_name_encoded).to_bytes(8, "big"),
                            old_name_encoded + new_name_encoded,
```
where
```python
old_name_encoded = old_name.encode("utf-8")
new_name_encoded = new_name.encode("utf-8")
```


Discussion:
cc @farizrahman4u @khustup 

Resolving patch:
diff --git a/deeplake/core/version_control/dataset_diff.py b/deeplake/core/version_control/dataset_diff.py
--- a/deeplake/core/version_control/dataset_diff.py
+++ b/deeplake/core/version_control/dataset_diff.py
@@ -36,10 +36,13 @@ def tobytes(self) -> bytes:
                         [
                             len(old_name).to_bytes(8, "big"),
                             len(new_name).to_bytes(8, "big"),
-                            (old_name + new_name).encode("utf-8"),
+                            (old_name + new_name),
                         ]
                     )
-                    for old_name, new_name in self.renamed.items()
+                    for old_name, new_name in map(
+                        lambda n: (n[0].encode("utf-8"), n[1].encode("utf-8")),
+                        self.renamed.items(),
+                    )
                 ),
                 len(self.deleted).to_bytes(8, "big"),
                 *(


Test patch:
diff --git a/deeplake/core/version_control/test/test_dataset_diff.py b/deeplake/core/version_control/test/test_dataset_diff.py
new file mode 100644
--- /dev/null
+++ b/deeplake/core/version_control/test/test_dataset_diff.py
@@ -0,0 +1,30 @@
+from deeplake.core.version_control.dataset_diff import DatasetDiff
+
+
+def test_tobytes():
+    diff = DatasetDiff()
+    diff.tensor_renamed("old1", "newer1")
+    diff.tensor_renamed("old2", "\u604f\u7D59")
+    diff.tensor_deleted("deleted1")
+    diff.tensor_deleted("deleted2")
+    diff.tensor_deleted("deleted3")
+
+    assert diff.tobytes() == b"".join(
+        [
+            False.to_bytes(1, "big"),
+            int(2).to_bytes(8, "big"),
+            len("old1".encode("utf-8")).to_bytes(8, "big"),
+            len("newer1".encode("utf-8")).to_bytes(8, "big"),
+            "old1newer1".encode("utf-8"),
+            len("old2".encode("utf-8")).to_bytes(8, "big"),
+            len("\u604f\u7D59".encode("utf-8")).to_bytes(8, "big"),
+            "old2\u604f\u7D59".encode("utf-8"),
+            int(3).to_bytes(8, "big"),
+            len("deleted1".encode("utf-8")).to_bytes(8, "big"),
+            "deleted1".encode("utf-8"),
+            len("deleted2".encode("utf-8")).to_bytes(8, "big"),
+            "deleted2".encode("utf-8"),
+            len("deleted3".encode("utf-8")).to_bytes(8, "big"),
+            "deleted3".encode("utf-8"),
+        ]
+    )