当前位置:网站首页>C语言标准函数scanf不安全的原因
C语言标准函数scanf不安全的原因
2022-07-05 08:32:00 【明月清风-精进不止】
scanf是标准C语言的标准库函数;
scanf_s是微软的,比scanf增加了一个参数,必须说明要输入的字符的个数;
eg:
char buf[5]={0};
scanf("%s",buf);//如果输入1234567890,则5后面的字符也会读入,可能会写入到其他变量的位置,导致程序
//异常,在VS2019下测试,程序崩溃. 如果输入12,则正常
char buf[5]={0};
scanf_s("%s",buf,4);//只能存储空间字符个数-1个字符 因为最后一个空间要存储\0
//在vs2019下测试,如果输入≤4个字符,一切正常,如果输入5个字符或者以上,程序不崩溃,但是buf中没有数据,原因应该是4个字符以内(含)没有接收到回车,所以就没写入到buf中。。
边栏推荐
- PIP installation
- 696. 计数二进制子串
- 猜谜语啦(6)
- More than 90% of hardware engineers will encounter problems when MOS tubes are burned out!
- Talk about the circuit use of TVs tube
- [cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl
- Typical low code apaas manufacturer cases
- How apaas is applied in different organizational structures
- MHA High available Cluster for MySQL
- Apaas platform of TOP10 abroad
猜你喜欢
随机推荐
剑指 Offer 06. 从尾到头打印链表
Detailed summary of FIO test hard disk performance parameters and examples (with source code)
MATLAB小技巧(28)模糊综合评价
Old Wang's esp8266 and old Wu's ws2818 light strip
猜谜语啦(6)
猜谜语啦(8)
Let's briefly talk about the chips commonly used in mobile phones - OVP chips
Brief discussion on Buck buck circuit
实例009:暂停一秒输出
[three tier architecture and JDBC summary]
Five design details of linear regulator
MySQL之MHA高可用集群
Apaas platform of TOP10 abroad
Explain task scheduling based on Cortex-M3 in detail (Part 2)
Meizu Bluetooth remote control temperature and humidity access homeassistant
How to copy formatted notepad++ text?
Example 001: the number combination has four numbers: 1, 2, 3, 4. How many three digits can be formed that are different from each other and have no duplicate numbers? How many are each?
Affected tree (tree DP)
Bluebridge cup internet of things basic graphic tutorial - GPIO output control LD5 on and off
2022.7.4-----leetcode. one thousand and two hundred