This is an automated email from the ASF dual-hosted git repository.
jmclean pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gravitino.git
The following commit(s) were added to refs/heads/main by this push:
new 46705b01a9 [#7957] improvement(namespace): make constructor
defensively (#7986)
46705b01a9 is described below
commit 46705b01a9f93c8b147227d9192534664d131b58
Author: 신동재 <[email protected]>
AuthorDate: Fri Aug 8 17:37:40 2025 +0900
[#7957] improvement(namespace): make constructor defensively (#7986)
### What changes were proposed in this pull request?
Namespace constructor parameter is an array, So apply defensive copying
to ensure immutability and add test code.
### Why are the changes needed?
Previously, modifying the input array externally could affect the
internal state of Namespace.
The levels() method also exposed the internal array, allowing unintended
external mutations.
Fix: #7957
### Does this PR introduce _any_ user-facing change?
No
### How was this patch tested?
```
@Test
public void testNamespaceImmutability() {
String[] levels = new String[] {"a", "b"};
Namespace ns = Namespace.of(levels);
// Modifying the original array should not affect the namespace
levels[0] = "x";
Assertions.assertEquals("a", ns.level(0));
// Modifications to the returned levels array should also not affect
the namespace
String[] returnedLevels = ns.levels();
returnedLevels[1] = "y";
Assertions.assertEquals("b", ns.level(1));
}
```
---
api/src/main/java/org/apache/gravitino/Namespace.java | 4 ++--
api/src/test/java/org/apache/gravitino/TestNamespace.java | 15 +++++++++++++++
2 files changed, 17 insertions(+), 2 deletions(-)
diff --git a/api/src/main/java/org/apache/gravitino/Namespace.java
b/api/src/main/java/org/apache/gravitino/Namespace.java
index 7561dd96ea..43400ee7d3 100644
--- a/api/src/main/java/org/apache/gravitino/Namespace.java
+++ b/api/src/main/java/org/apache/gravitino/Namespace.java
@@ -87,7 +87,7 @@ public class Namespace {
}
private Namespace(String[] levels) {
- this.levels = levels;
+ this.levels = Arrays.copyOf(levels, levels.length);
}
/**
@@ -96,7 +96,7 @@ public class Namespace {
* @return The levels of the namespace
*/
public String[] levels() {
- return levels;
+ return Arrays.copyOf(levels, levels.length);
}
/**
diff --git a/api/src/test/java/org/apache/gravitino/TestNamespace.java
b/api/src/test/java/org/apache/gravitino/TestNamespace.java
index dddc7a16a9..24811a0429 100644
--- a/api/src/test/java/org/apache/gravitino/TestNamespace.java
+++ b/api/src/test/java/org/apache/gravitino/TestNamespace.java
@@ -61,4 +61,19 @@ public class TestNamespace {
Assertions.assertThrows(IllegalArgumentException.class, () ->
Namespace.fromString("a."));
Assertions.assertThrows(IllegalArgumentException.class, () ->
Namespace.fromString("a..b"));
}
+
+ @Test
+ public void testNamespaceImmutability() {
+ String[] levels = new String[] {"a", "b"};
+ Namespace ns = Namespace.of(levels);
+
+ // Modifying the original array should not affect the namespace
+ levels[0] = "x";
+ Assertions.assertEquals("a", ns.level(0));
+
+ // Modifications to the returned levels array should also not affect the
namespace
+ String[] returnedLevels = ns.levels();
+ returnedLevels[1] = "y";
+ Assertions.assertEquals("b", ns.level(1));
+ }
}