Note: Do not expose map. Entry to untrusted code map. entryset ()

Source: Internet
Author: User

MAP/hashmap is a common data structure in Java. Generally, we do this in applications by calling put to write data to the container or get to read data from the container. The map. entryset () method returns a set of key-value pairs, which is also recommended by JDK to traverse the map.

Set<Map.Entry<String, String>> allEntrys = maps.entrySet();for (Map.Entry<String, String> as : allEntrys){    String key = as.getKey();String value = as.getValue();}


However, we should not pass the returned results of map. entryset () to untrusted code. Why? Let's take a look at the following code:

Public static void main (string [] ARGs) throws exception {hashmap <string, string> maps = new hashmap <string, string> (); maps. put ("name", "Xiu"); maps. put ("Age", "25"); system. out. println (MAPS); // {age = 25, name = Xiu} set <map. entry <string, string> allentrys = maps. entryset (); map. entry <string, string> nameentry = NULL; For (map. entry <string, string> as: allentrys) {string key =. getkey (); If (key. equals ("name") {nameentry = as ;}// Delete entry allentrys. remove (nameentry); system. out. println (MAPS); // {age = 25 }}
Obviously, we The returned results of map. entryset () can be used to delete the key-value pairs stored in the original hashmap.. If we pass set <map. Entry <string, string> allentrys as function parameters to untrusted code, malicious code can delete the data stored in the original hashmap. Therefore, we should avoid passing set <map. Entry <string, string> as function parameters to prevent external code from maliciously or accidentally modifying the original data. Not all Java programmers know this hidden function, so pay attention to it to avoid programming errors.


Note: Do not expose map. Entry to untrusted code map. entryset ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.