OSDN Git Service

p2p: check the length of the network name bytes
authorJimmy Chen <jimmycmchen@google.com>
Fri, 15 Nov 2019 02:06:27 +0000 (10:06 +0800)
committerJimmy Chen <jimmycmchen@google.com>
Wed, 11 Dec 2019 06:43:54 +0000 (06:43 +0000)
Fixes: 144472136
Bug: 144472136
Test: atest FrameworksWifiApiTests
Change-Id: I80b8e0330499e6c655efb1712811891a8e8229cb
(cherry picked from commit a0900344f4ea850ca9ee81fc936298f894e77f54)

wifi/java/android/net/wifi/p2p/WifiP2pConfig.java
wifi/tests/src/android/net/wifi/p2p/WifiP2pConfigTest.java

index c3cfb02..80776fe 100644 (file)
@@ -28,6 +28,7 @@ import android.text.TextUtils;
 
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
+import java.nio.charset.StandardCharsets;
 import java.util.regex.PatternSyntaxException;
 
 /**
@@ -228,6 +229,10 @@ public class WifiP2pConfig implements Parcelable {
 
         private static final MacAddress MAC_ANY_ADDRESS =
                 MacAddress.fromString("02:00:00:00:00:00");
+        /**
+         * Maximum number of bytes allowed for a SSID.
+         */
+        private static final int MAX_SSID_BYTES = 32;
 
         private MacAddress mDeviceAddress = MAC_ANY_ADDRESS;
         private String mNetworkName = "";
@@ -279,6 +284,10 @@ public class WifiP2pConfig implements Parcelable {
                 throw new IllegalArgumentException(
                         "network name must be non-empty.");
             }
+            if (networkName.getBytes(StandardCharsets.UTF_8).length > MAX_SSID_BYTES) {
+                throw new IllegalArgumentException(
+                        "network name exceeds " + MAX_SSID_BYTES + " bytes.");
+            }
             try {
                 if (!networkName.matches("^DIRECT-[a-zA-Z0-9]{2}.*")) {
                     throw new IllegalArgumentException(
index 41f109a..6199325 100644 (file)
@@ -53,6 +53,13 @@ public class WifiP2pConfigTest {
             fail("Unexpected IllegalArgumentException");
         }
 
+        // sunny case with maximum bytes for the network name
+        try {
+            b.setNetworkName("DIRECT-abcdefghijklmnopqrstuvwxy");
+        } catch (IllegalArgumentException e) {
+            fail("Unexpected IllegalArgumentException");
+        }
+
         // less than 9 characters.
         try {
             b.setNetworkName("DIRECT-z");
@@ -77,6 +84,12 @@ public class WifiP2pConfigTest {
             b.setNetworkName("direct-a?");
             fail("expected IllegalArgumentException");
         } catch (IllegalArgumentException e) { }
+
+        // over maximum bytes
+        try {
+            b.setNetworkName("DIRECT-abcdefghijklmnopqrstuvwxyz");
+            fail("expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) { }
     }
 
     /**