当前位置:网站首页>Shit, someone poisoned my code comments?
Shit, someone poisoned my code comments?
2022-04-22 02:14:00 【Morning Xi light】
background
One day , I wrote the following line of comment in my code , What surprised me was that the code in the comment was executed .

According to the conventional idea , This line of code is annotated , The console should not have execution results , However, the following results are returned .

Come here , We can probably guess that Unicode Decoding occurs before any word decoding . and \u000d It's a newline , Therefore, the comment is terminated, resulting in the execution of the comment code after the newline character .
What is? Unicode The escape ?
I went to oracle Have a look at the official website Java language norm (JLS 3) Related explanation , The main idea is as follows :Unicode Escape is used to indicate that only ASCII Character Unicode Symbol . When you need to insert characters that cannot be represented in the character set of the source file , It will come in handy .JLS 3.3 Relevant instructions in section ,Unicode The escape contains a backslash character (\), One or more heels 'u' Characters and four hexadecimal digits .

therefore , In the example \u000d Will be treated as a newline .
The figure below shows the official JLS file .

The advantage of this mechanism is that it can be used in ASCII And any other code , And you don't need to figure out where the comments start and end !
Analysis and verification
To confirm whether it's what we analyzed , I used it Java Built-in tools native2ascii To convert a file with any supported character encoding to a file with ASCII or Unicode Escaped file .

Execute the following command at a glance .


I can find that the converted code has been wrapped !
Actually , I can also check class Bytecode to find the clue .

It seems to be the same as what we analyzed before .
summary
This operation also ensures Java The core idea —— Platform consistency .
Although the processing mechanism in this way seems elegant , however , It has side effects ( Interference semantics ), Especially in the comments , We must pay attention to !
Okay , Leave a question for you to think about , Create a new one Hi.java file , The following Unicode Copy code to file , See what the execution result will be ?
版权声明
本文为[Morning Xi light]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220207565371.html
边栏推荐
- cv2. What is the custom format of drawcontours()
- Precise timing, GPS Beidou satellite timing synchronization clock system ceiling
- error:there‘s no Qt version assigned to project please assign a Qt installation in qt project settin
- 51 page digital transformation and informatization planning of the 14th five year plan
- Code standardization solution for project construction
- [programming question] construct palindrome
- Jetcode prize essay solicitation activity | interesting stories of low code industry waiting for you to share
- C reads data from the database and exports it to CSV
- JMeter + Jenkins + ant persistence
- flutter 不用状态栏的导航栏
猜你喜欢

Leetcode 141, circular linked list

How to select the appropriate neo4j Version (2022)

Page 58 Siemens digital solutions for machine tool industry

Zuo Chengyun - Dachang brushing class - laying bricks
![[check which package provides the installed packages and commands]](/img/d8/ce76b177937b46c12469ea2bddf565.png)
[check which package provides the installed packages and commands]

Redis 缓存数据库使用 Redis-Shake 做数据同步

IP message analysis notes

Leetcode1470. Rearrange array

In PostgreSQL, convert a string to an integer or floating-point type in the query result

树、二叉树和森林的转换
随机推荐
Page 107 planning and design of enterprise digital transformation
Advanced formula 43 of C language: the meaning of function
(counting line segment tree) lintcode medium 248 · count the number of numbers smaller than a given integer
Raspberry pie 4B 8g installation log (3) - Programming Environment
Method for extracting middle layer output of CNN model
68 smart pipe gallery project construction solution
Good news, Jingzhun GPS Beidou timing system helps South Asia electronic management system
Leetcode 203. Remove linked list elements
MySQL -- master slave synchronization
黑夜也能五颜六色,用深度学习实现全彩夜视系统
Advanced formula 44 of C language: the secret of function parameters (Part 1)
Opencv 4.0 learning notes
Hj6 prime factor
高级android面试答案,Gradle源码全解析
Leetcode 567. Arrangement of strings
HJ5 进制转换
Leetcode 566, remodeling matrix
Detailed explanation of transaction isolation level
QT程序打包成一个exe可执行文件
Mysql database fields move up and down