valgrind

8个有用的C语言常见面试题

放肆的年华 提交于 2020-08-16 02:03:25
  8个C语言面试题,涉及指针、进程、运算、结构体、函数、内存   1.strcpy()函数   问:下面是一个简单的密码保护功能,你能在不知道密码的情况下将其破解吗?   file:///C:\Users\Administrator.WIN-STED6B9V5UI\AppData\Local\Temp\ksohtml2668\wps20.png   答:破解上述加密的关键在于利用攻破strcpy()函数的漏洞。所以用户在向“passwd”缓存输入随机密码的时候并没有提前检查“passwd”的容量是否足够。所以,如果用户输入一个足够造成缓存溢出并且重写“flag”变量默认值所存在位置的内存的长“密码”,即使这个密码无法通过验证,flag验证位也变成了非零,也就可以获得被保护的数据了。例如:   file:///C:\Users\Administrator.WIN-STED6B9V5UI\AppData\Local\Temp\ksohtml2668\wps21.png   虽然上面的密码并不正确,但我们仍然可以通过缓存溢出绕开密码安全保护。   要避免这样的问题,建议使用 strncpy()函数。   作者注:最近的编译器会在内部检测栈溢出的可能,所以这样往栈里存储变量很难出现栈溢出。在我的gcc里默认就是这样,所以我不得不使用编译命令‘-fno-stack-protector

Android使用valgrind内存泄漏和越界检测等

自闭症网瘾萝莉.ら 提交于 2020-08-06 09:56:57
valgrind是一个强大的工具,最常用的功能是用它来检测内存泄漏和非法内存的使用。 测试程序编译时,需要加-g进行编译。 运行命令: # valgrind --tool=memcheck --leak-check=yes ./test_demo 生成如下报告: 1. 如果malloc/realloc/calloc和free的数量不同,则会报告如下的内容。 ==3375== HEAP SUMMARY: ==3375== in use at exit: 128 bytes in 1 blocks ==3375== total heap usage: 4,900 allocs, 4,899 frees, 29,477,380 bytes allocated 2. 如果有第一个问题,则会报告哪里申请的内存没有进行释放: ==3375== 128 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==3375== at 0x4C2AC3D: dd (d.c:299) ==3375== by 0x50C44F2: cc (c.c:112) ==3375== by 0x5211824: bb (b.c:526) ==3375== by 0x518643B: aa (a.c:398) ==3375== by 0x400EB3:

How to resolve memory related errors that arise from interaction between C objects in a C++ wrapper?

我与影子孤独终老i 提交于 2020-07-10 07:44:27
问题 The problem I am writing a thin C++ wrapper around an object oriented C library. The idea was to automate memory management, but so far its not been very automatic. Basically when I use my wrapper classes, I get all kinds of memory access and inappropriate freeing problems. Minimal example of C library Lets say the C library consists of A and B classes, each of which have a few 'methods' associated with them: #include <memory> #include "cstring" #include "iostream" extern "C" { typedef struct

How to run Valgrind to find out memory leaks on my Embedded MIPSEL- linux box?

笑着哭i 提交于 2020-07-06 03:47:11
问题 How can I run valgrind on an embedded Linux box to find memory leaks in my main software? In the rcS script, I am running like this: ./main_app How can I associate the ./main_app program with valgrind? The main_app process never terminates. I want to constantly log the data to file. Also, I want to access the log file without terminating the main_app process. I can do telnet and can access the log file. But the problem is until and unless the handler is closed, how can I open the file i.e. I

How to run Valgrind to find out memory leaks on my Embedded MIPSEL- linux box?

拈花ヽ惹草 提交于 2020-07-06 03:40:02
问题 How can I run valgrind on an embedded Linux box to find memory leaks in my main software? In the rcS script, I am running like this: ./main_app How can I associate the ./main_app program with valgrind? The main_app process never terminates. I want to constantly log the data to file. Also, I want to access the log file without terminating the main_app process. I can do telnet and can access the log file. But the problem is until and unless the handler is closed, how can I open the file i.e. I

How to run Valgrind to find out memory leaks on my Embedded MIPSEL- linux box?

故事扮演 提交于 2020-07-06 03:39:58
问题 How can I run valgrind on an embedded Linux box to find memory leaks in my main software? In the rcS script, I am running like this: ./main_app How can I associate the ./main_app program with valgrind? The main_app process never terminates. I want to constantly log the data to file. Also, I want to access the log file without terminating the main_app process. I can do telnet and can access the log file. But the problem is until and unless the handler is closed, how can I open the file i.e. I

can helgrind (valgrind) be used with c++11 futures

☆樱花仙子☆ 提交于 2020-06-26 04:35:45
问题 I'm getting what I think are false positives when using helgrind with C++11 futures and packaged tasks. The following is with gcc-6.3.0 and valgrind-3.12 on a CentOS6 system. I've tried to follow the advice in the documentation to provide annotations. Have I done something wrong? What should I do to avoid the false positives, or is there really a race? drdws0134$ cat hthread.cpp #include <valgrind/helgrind.h> #define _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(addr) ANNOTATE_HAPPENS_BEFORE(addr)

Memory/Address Sanitizer vs Valgrind

安稳与你 提交于 2020-06-24 05:42:08
问题 I want some tool to diagnose user-after-free bugs and uninitialized bugs. I am considering Sanitizer(Memory and/or Address) and Valgrind. But I have very little idea about their advantages and disadvantages. Can anyone tell the main features, differences and pros/cons of Sanitizer and Valgrind? Edit: I found some of comparisons like: Valgrind uses DBI(dynamic binary instrumentation) and Sanitizer uses CTI(compile-time instrumentation). Valgrind makes the program much slower(20x) whether

Why doesn't valgrind detect a memory leak in my “test” program?

╄→гoц情女王★ 提交于 2020-06-11 20:05:34
问题 The entire test code is contained in main.cpp as follows: #include <iostream> using std::cout; using std::endl; void f(int i) { int* pi = new int; *pi = i; std::cout << "*pi = " << *pi << std::endl; } int main(int argc, char *argv[]) { int i = 0; while (i < 10000) { f(i); ++i; } return 0; } I compile with no optimizations -O0 (from an Eclipse Qt project) with: g++ -c -pipe -O0 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I

Valgrind showing error calling pr_set_ptracer, vgdb might block

我的梦境 提交于 2020-05-14 19:10:53
问题 I am using Valgrind to find memory leaks of my C program and although it looks like it is running fine and showing allocated and freed memory. But, I want to know why it is throwing this error and what are its consequences. Here the snippet of error: ==483== Memcheck, a memory error detector ==483== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==483== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info ==483== Command: ./main ../old\ projects ==483== ==483==