The flaw of the Eval function

Source: Internet
Author: User

1, today in the maintenance of the product catalog, found a wonderful thing, the Eval function conversion error, after the inspection found in the string if a newline character added will cause the problem.

2. Test procedure

Info.file

[{productdirids: ' 1 ', Productdirname: ' PC server '},{attributes: ' Hello

Nice '}]

public static void Main (string[] args) throws FileNotFoundException {
Scanner sc = new Scanner (New File (info.file "));
StringBuffer sb = new StringBuffer ();
Sb.append ("var json =eval (");
while (Sc.hasnextline ()) {
Sb.append (Sc.nextline ());
Sb.append ("\ n");//Note that comments out here can be performed normally, but if you add this sentence will cause an error, and under IE, the F12 console is not able to see the error.
}
Sb.append (");");
Sc.close ();
System.out.println (Sb.tostring ());
Sb.append ("Json[1].attributes");
Scriptenginemanager manager = new Scriptenginemanager ();
ScriptEngine engine = Manager.getenginebyname ("javascript");
try{
String Dynamicvalue = Engine.eval (sb.tostring ()). ToString ();
System.out.println (Dynamicvalue);
}catch (Scriptexception e) {
E.printstacktrace ();
}
}

The flaw of the Eval function

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.