当前位置:网站首页>On the insecurity of using scanf in VS
On the insecurity of using scanf in VS
2022-04-23 14:25:00 【KissKernel】
# About the vs Use in scanf The problem of insecurity
### First of all scanf Why is it not safe
The first is about memory overflow ,scanf The length of the string you enter will not be detected when using, so there is a risk of memory overflow .
#include<stdio.h>
int main()
{
char arr[6]={
0};
scanf("%s",&arr);
// Assume that the input helloworld There will be the problem of cross-border visits , therefore scanf In this case, it will not be safe .
}
### The second is how to solve scanf The problem of insecurity
There are two ways to solve this problem
1. One is to open vs project Options see the last option attribute
take SDL Check disable
2. In the file .c Insert the following code in the first line of the ;
#define _CRT_SECURE_NO_WARNINGS
Inserting at the beginning of each file is obviously a little cumbersome , So there's another way... Once and for all , Let me introduce it . First find your vs Install path to find this file newc++file.c Copy it to the desktop, and then copy the above code in , Then copy this file back to the original path . Replace the original file .* So why don't you do it here , Because when you change directly in the original path newc++file.c You will be prompted that you do not have permission to change .
### Finally, why scanf_s Security
vs Provided in scanf_s The function needs to specify the number of input characters , So you need to enter an additional qualified number . This ensures that the function will not access beyond the bounds .
#include<stdio.h>
int main()
{
char arr[6];
scanf_s("%s",&arr,5);// Then there will be no unexpected cross-border visits at this time
return 0;
}
There will only be man-made cross-border access, such as you have to enter hello Five characters only define arr[5] So the end of the string \0; It will cross the border because there is no place to place it .
Of course, I still recommend using... In daily programming scanf because scanf_s yes vs If you write it yourself, it will report errors in other compilers , Poor reusability .
I uploaded all the above files in my code cloud , You can see what you need
link
版权声明
本文为[KissKernel]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231412251974.html
边栏推荐
- JS recursion (1)
- Redis源码分析之PSYNC同步
- KVM学习资源
- MySQL同步Could not find first log file name in binary log index file错误
- Introduction to loan market quotation interest rate (LPR) and loan benchmark interest rate
- uni-app消息推送
- Pass in external parameters to the main function in clion
- Qt界面优化:鼠标双击特效
- Usage of BC
- Processing MKDIR: unable to create directory 'AAA': read only file system
猜你喜欢
如何5分钟上手使用OCR
API Gateway/API 网关(四) - Kong的使用 - 集成Jwt和熔断插件
基于单片机的DS18B20的数字温度监控报警系统设计【LCD1602显示+Proteus仿真+C程序+论文+按键设置等】
一篇博客让你学会在vscode上编写markdown
51单片机+LCD12864液晶显示的俄罗斯方块游戏,Proteus仿真、AD原理图、代码、论文等
sar命令详解
Multisim Simulation Design of DC adjustable regulated power supply of LM317 (with simulation + paper + reference)
c语言在结构体传参时参数压栈问题
C语言知识点精细详解——初识C语言【1】——你不能不知的VS2022调试技巧及代码实操【1】
API Gateway/API 网关(三) - Kong的使用 - 限流rate limiting(redis)
随机推荐
Four ways of SSH restricting login
一篇博客让你学会在vscode上编写markdown
Basic regular expression
Qt实战:云曦聊天室篇
八路抢答器系统51单片机设计【附Proteus仿真、C程序、原理图及PCB文件、元器件清单和论文等】
Introduction to the use of countdownlatch and cyclicbarrier for inter thread control
asp.net使用MailMessage发送邮件的方法
金九银十,入职字节跳动那一天,我哭了(蘑菇街被裁,奋战7个月拿下offer)
Pass in external parameters to the main function in clion
API Gateway/API 网关(二) - Kong的使用 - 负载均衡Loadbalance
Gif to still image processing
Proteus simulation design of four storey and eight storey elevator control system, 51 single chip microcomputer, with simulation and keil c code
ssh限制登录的四种手段
操作系统常见面试题目:
统信UOS PHP7.2.3升级至PHP7.2.24
如何5分钟上手使用OCR
关于UDP接收icmp端口不可达(port unreachable)
Flop effect
Some little records~
Multisim Simulation Design of DC adjustable regulated power supply of LM317 (with simulation + paper + reference)