当前位置:网站首页>Sscanf caused the address to be out of bounds
Sscanf caused the address to be out of bounds
2022-07-27 21:02:00 【Li-Yongjun】
problem
test.c
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int a = 100;
uint8_t b;
sscanf("0x15", "0x%x", &b);
printf("a = 0x%x\n", a);
printf("b = 0x%x\n", b);
return EXIT_SUCCESS;
}
$ ./test.out
a = 0x0
b = 0x15
a Why does the value of become 0 What about it ?
answer
because sscanf In giving b assignment , Because the specified parameter format is %x, therefore sscanf Think b It's a unsigned int type , So the &b Transformed into unsigned int * type , This is giving b assignment , According to unsigned int Such a large space for assignment , As a result, the memory of other variables is manipulated , Cause program problems .
The figure below a) It's what we expect , But because of “%x”, Lead to &b “ Control and control ” The space of is expanded from one byte to four bytes ,“ Embezzlement ” Variable a Space , Pictured b), Lead to a The value of is tampered with .
avoid
Actually at compile time , The compiler has already issued a warning . So when we write code , Always pay attention to the warnings reported by the compiler , It can help us correct many mistakes .
therefore , One principle of submitting code is : Try not to introduce new warnings .
$ gcc test.c -o test.out
test.c: In function ‘main’:
test.c:10:21: warning: format ‘%x’ expects argument of type ‘unsigned int *’, but argument 3 has type ‘uint8_t *’ {
aka ‘unsigned char *’} [-Wformat=]
10 | sscanf("0x15", "0x%x", &b);
| ~^ ~~
| | |
| | uint8_t * {
aka unsigned char *}
| unsigned int *
| %hhx
Recommend writing
Method 1 : Use "%hhx"
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int a = 100;
uint8_t b;
sscanf("0x15", "0x%hhx", &b);
printf("a = 0x%x\n", a);
printf("b = 0x%x\n", b);
return EXIT_SUCCESS;
}
$ ./test.out
a = 0x64
b = 0x15
Method 2 : Use int Dump intermediate variables of type .
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int a = 100;
uint8_t b;
int c;
sscanf("0x15", "0x%x", &c);
b = (uint8_t)c;
printf("a = 0x%x\n", a);
printf("b = 0x%x\n", b);
return EXIT_SUCCESS;
}
$ ./test.out
a = 0x64
b = 0x15
边栏推荐
- Hexagon_V65_Programmers_Reference_Manual(9)
- SQL coding bug
- 程序放在哪儿?
- Source Insight 4.0使用介绍
- 说透缓存一致性与内存屏障
- CPDA | how to have data analysis thinking?
- [deep learning] pytoch torch Autograd automatic differential engine
- Where is the program?
- R语言使用lm函数构建多元回归模型(Multiple Linear Regression)、并根据模型系数写出回归方程、使用deviance函数计算出模型的残差平方和
- Ipv4/ipv6, DHCP, gateway, routing
猜你喜欢

Best practices for Oracle kingbasees migration of Jincang database (4. Oracle database migration practice)

Face recognition 5.1- insightface face face detection model training practice notes

How to improve the picture transmission speed and success rate in the development of IM instant messaging under the mobile network

SLIM:自监督点云场景流与运动估计(ICCV 2021)

82. (cesium article) cesium points move on 3D models

VI working mode (3 kinds) and mode switching (conversion)

如何查看蓝牙耳机的蓝牙版本

你了解数据同步吗?

To do the test, you have to go to the big factory and disclose the "hidden rules" of bat big factory recruitment internally

API Gateway介绍
随机推荐
原生对象、内置对象、宿主对象的区别
After working for bytek for two years, he got 15 offers at one go
Uncaught SyntaxError: redeclaration of let page
Go --- automatic recompilation of air
基于文件上传漏洞获得网站 shell 权限
走马灯案例
Slim: self supervised point cloud scene flow and motion estimation (iccv 2021)
一文了解Pycharm快捷键
Riding lantern case
Force deduction solution summary 592 fraction addition and subtraction
如何查看蓝牙耳机的蓝牙版本
【R语言】【1】初学R语言语法使用Rstudio编辑
Codeforces 1706E 并查集 + 启发式合并 + ST 表
js闭包知识
金仓数据库 Oracle至KingbaseES迁移最佳实践(2. 概述)
Hexagon_V65_Programmers_Reference_Manual(9)
MySQL驱动jar包的下载--保姆教程
R语言使用lm函数构建多元回归模型(Multiple Linear Regression)、并根据模型系数写出回归方程、使用deviance函数计算出模型的残差平方和
go --- air自动重新编译
“收割”NFT:200元淘宝买图,上链卖30万元