简体   繁体   中英

Valgrind reporting invalid write errors

This is more focused than my previous Valgrind question; I am trying to narrow down write and read errors when parsing command-line options:

#include <stdio.h>                                                                                                                                                                                                                    
#include <stdlib.h>                                                                                                                                                                                                                   
#include <unistd.h>                                                                                                                                                                                                                   
#include <sys/utsname.h>                                                                                                                                                                                                              
#include <sys/stat.h>                                                                                                                                                                                                                 
#include <string.h>                                                                                                                                                                                                                   
#include <locale.h>                                                                                                                                                                                                                   
#include <bzlib.h>                                                                                                                                                                                                                    
#include <zlib.h>                                                                                                                                                                                                                     
#include "starch.h"

#define BUFMAXLEN 1024

int main(int argc, char **argv) { 
    if (parseCommandLineInputs( &argc, &argv ) != 0)
        exit(EXIT_FAILURE);

    return 0;
} 

int parseCommandLineInputs(int *argc, char ***argv) {

    pid_t pid;                                                                                                                                                                                                                        
    struct utsname uts;
    char uniqTag[BUFMAXLEN];

    if ((*argc == 1) || (*argc > 4)) {                                                                                                                                                                                                
        printUsage();
        return -1;                                                                                                                                                                                                           
    }                                                                                                                                                                                                                                  

    if ((pid = getpid()) < 0) {                                                                                                                                                                                                       
        fprintf(stderr, "\n\t[starch] - Error: Could not obtain process ID\n\n");                                                                                                                                                     
        return -1;                                                                                                                                                                                                           
    }                                                                                                                                                                                                                                 
    uname( &uts );                                                                                                                                                                                                                    
    sprintf(uniqTag, "pid%d.%s", pid, uts.nodename);                                                                                                                                                                                  

    switch (*argc) {                                                                                                                                                                                                                  
        case 2: {                                                                                                                                                                                                                     
            if (strcmp(*argv[1], "-") != 0) {                                                                                                                                                                                         
                if (fileExists(*argv[1]) != 0) { /* standard input */                                                                                                                                                              
                    ...
    }

    return 0;
}


int fileExists(char *fn) {
    struct stat buf;
    int i = stat (fn, &buf);
    if (i == 0)
        return 0;
    return -1;
}

void printUsage() {
    fprintf(stderr, "my usage statement\n\n");
}

My makefile is as follows:

CC      = gcc                                                                                                                                                                                                                         
CFLAGS  = -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE=1 -DUSE_ZLIB -O3 -Wformat -Wall -pedantic -std=gnu99 -g                                                                                                                        
BIN     = ../bin                                                                                                                                                                                                                      

all: starch                                                                                                                                                                                                                           
    rm -rf *~                                                                                                                                                                                                                         

starch: starch.o                                                                                                                                                                                                                      
    mkdir -p $(BIN) && $(CC) ${CFLAGS} starch.o -lbz2 -lz -o ${BIN}/starch                                                                                                                                                            
    rm -rf *~                                                                                                                                                                                                                         

clean:
    rm -rf *.o *~ ${BIN}/starch

I get the following errors when I run with valgrind :

$ valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes -v ../bin/starch

...

==2675== 1 errors in context 1 of 2:                                                                                                                                                                                                  
==2675== Invalid read of size 8                                                                                                                                                                                                       
==2675==    at 0x3AB4262243: fwrite (in /lib64/libc-2.5.so)                                                                                                                                                                           
==2675==  Address 0x7fedffd68 is on thread 1's stack                                                                                                                                                                                  
==2675==                                                                                                                                                                                                                              
==2675==                                                                                                                                                                                                                              
==2675== 1 errors in context 2 of 2:                                                                                                                                                                                                  
==2675== Invalid write of size 8                                                                                                                                                                                                      
==2675==    at 0x401AA6: parseCommandLineInputs (starch.c:217)                                                                                                                                                                        
==2675==    by 0x7FF0000AF: ???                                                                                                                                                                                                       
==2675==    by 0x401DFA: main (starch.c:46)                                                                                                                                                                                           
==2675==  Address 0x7fedffd68 is on thread 1's stack 

The first error is not telling me anything I can use, since I am not using fwrite() anywhere.

The second error is tripped up on the fprintf statement in printUsage() .

Line 46 is the following line:

 if (parseCommandLineInputs( &argc, &argv ) != 0)

Line 217 is the following line:

 fprintf(stderr, "my usage statement\n\n");

What is wrong with my application that explains why these errors appear?

Two things that pop up to me right away:

  • *argv[1] is NOT the same as (*argv)[1] which is what you probably mean. Array subscripting has precedence over pointer dereferencing. This results in an invalid pointer. As many experienced programmers will tell you: "Don't try to remember the precedence of operators - if in doubt use parentheses, if not just use them anyway".

  • -O3 in the compiler flags is a big fat NO-NO when debugging. The compiler will mangle your code so much that it can make your life impossible. Variables can disappear completely and functions can go mysteriously away as they get inlined. If your code compiles, links and runs with -O0 (IIRC some code containing inline assembly needs -O1 with some (all?) GCC versions) use it, otherwise use -O1 at most.

There is too much missing so it is not easy to tell what is going on. I suppose that pid is pid_t ? The only thing that I then see is this one:

sprintf(uniqTag, "pid%d.%s", pid, uts.nodename);

pid_t is not necessarily an int so sprintf might be on the wrong track when parsing its arguments and messup your stack. But gcc should have have told you if you compile with -Wall , which I suppose.

Try compiling with a different compiler, clang eg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM