CC ++ Memory Question Terrible - Purify (2)

zhaozj2021-02-08  206

Third, example

Suppose we have such a program now: hello.c

#include

#include

Static char * helloworld = "Hello, World";

Main ()

{

Char * mystr = malloc (Strlen (HelloWorld);

STRNCPY (MySTR, HelloWorld, 12);

Printf ("% s / n", mystr);

}

Obviously, there is a memory in this program, suppose we neglect these errors. When we use Purify to test, we will find that the memory error in this program is easy to find under Purify. First, we have to do this program using Purify:

> purify gcc -g -o hello hello.c

PURIFY 2003.06.00 Solaris 2 (32-BIT) Copyright (C) 1992-2002 Rational Software Corp. All Rights Reserved.

Instrumenting: cckc6prud.o linking

Remember to add "-g" options, otherwise, PURIFY cannot display the source program. Ok, now in the current directory, you have an executable file - before running Hello, we have to pay attention to executing procedures compiled by Purify, so you may have X-windows, so please pay attention to setting up the Display environment. If you are executing under Windows Telnet client, then you can put a tool software called Exceed in Windows, which can easily display X-WINDOW on your Windows desktop)

Ok, we can now execute the Hello program, you can see one of the windows below:

We can see that there are two memory errors in the Purify report, one is the ABR (Array Bounds Read) - an array of groups, one is a 12-byte Memory Leaked, expand the small triangle symbol, we can see more detailed report:

After the ABR error is expanded, we can see that the generation of ABR errors is generated by Printf, and the memory that generates errors is MyStr. By observing, we can immediately find what will appear ABR errors because the strings in C / C are ending with "/ 0", while we allocate strings, it should be more than the actual length. Store the end of the end, and the Strncpy calls later only copies the valid content of the string, and does not end the end of the string. The system calls the PrintF output string content until the end of the end is encountered, so when it accesses 12 lengths, it has not been discovered, so it continues to be accessed down, and an ABR error occurs.

Ok, let's take a look at the report of Memory Leaked:

We can see that Purify pointed out that the memory has been disclosed, how many bytes are leaked. Through the report of Purify, add our understanding of the C / C foundation. We immediately know that mystr is allocated on the heap, so we must manually release, view the program, we found we forgot Free (MyStr).

Ok, now we can modify our programs based on the Purify report:

#include #include

Static char * helloworld = "Hello, World";

Main ()

{

Char * mystr = malloc (Strlen (HelloWorld) 1);

STRNCPY (MySTR, HelloWorld, 12);

Mystr [12] = "/ 0";

Printf ("% s / n", mystr);

Free (mystr);

}

Now, we use purify to recompile our program, then run, we can see that Purify will report without any memory issues. In fact, using Purify is very simple, behind, I will comprehensively explain the various common characteristics of Purify.

Fourth, a list of memory issues

Below is the memory information table that Purify can detect:

Memory information

description

Error rating

ABR ARRAY BOUNDS READ Array Boundary Read

Level 3 ABW Array Bounds Write array

Level 2 BSR BEYOND Stack Read

Level 3 BSW Beyond Stack Write Yue Stack Write

Level 3 COR CORE DUMP IMMINENT illegal operation

Level 1 FIU File Descriptors in Use file descriptor is used

Level 4 FMM Freeing Mismatch Memory Release Error Memory

Level 2 FMR Free Memory Read is read within the released

Level 3 FMW Free Memory Write writes within the released memory

Level 2 FNH Freeing Non Heap Memory releases non-reactor memory

Level 2 Fum Freeing Unallocated Memory released the memory that was not assigned

Level 2 IPR Invalid Pointer Read illegal pointer read

Level 1 IPW INVALID POINTER WRITE illegal

Level 1 MAF Malloc Failure Assignment Instruction Failed

Level 4 Miu Memory in-USE memory is being used

Level 4 MLK Memory Leak Memory Leakage

Level 3 MRE Malloc Reentrance Error Remalloc

Level 2 MSE Memory Segment Error memory segment error

Level 3 NPR NULL POINTER READ Empty Index Read

Level 1 NPW NULL POINTER WRITE Empty Guide

Level 1 PAR BAD PARAMETER Error Parameters

Level 3 PLK Potential Leak Potential Memory Lev

Level 3 SBR Stack Array Bounds READ Stack Array Crossing Read

Level 3 SBW Stack Array Bounds WRITE Stack Arkline Write

Level 2 SIGNAL signal

Level 4 SOF Stack Overflow Stack overflow

Level 3 UMC Uninitialized Memory Copy copies uninitialized memory

Level 3 UMR Uninitialized Memory Read to uninited memory read

Level 3 WPF WatchPoint Free Releases Monored Memory

Level 4 WPM WatchPoint Malloc Monitor Memory Allocation

Level 4 WPN WatchPoint Entry Monitoring Memory

4 WATCHPOINT READ monitored memory read

Level 4 WPW WatchPoint Write Monitored memory

Level 4 WPX WatchPoint EXIT exits the monitored memory

Level 4 ZPR ZERO Page Read Zero Pedes

Level 1 ZPW Zero Page Write Zero Page 1 Level 1

Level 1: fatal mistakes. Level 2: dangerous mistakes. Level 3: Warning Information Level 4: Tips Information (non-wrong)

<- Previous Next -> (All rights reserved, please indicate the author and source)

转载请注明原文地址:https://www.9cbs.com/read-1153.html

New Post(0)