c strstr() producing invalid memory addresses

This is a discussion on c strstr() producing invalid memory addresses within the C Programming forums, part of the General Programming Boards category; Hello everyone, I'm Jesse and new to the forum. I'm also quite new to C programming and I have a ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    3

    c strstr() producing invalid memory addresses

    Hello everyone,

    I'm Jesse and new to the forum.
    I'm also quite new to C programming and I have a problem when dereferencing a char* from strstr().

    I am trying to program an rss bencode html translater for minix for an OS-assignment. The problem is not related, however. Every time I dereference the char* that strstr() produces I get the following error at runtime:

    VM: pagefault: SIGSEGV 36686 bad addr 0x64 (dataseg); err 0x4 nopage read
    PM: coredump signal 11 for 145 / rssParse
    rssPars 36686 0x4a0b 0x3700 0x206e 0x212d 0x2277 0x100a
    Memory fault (core dumped)


    Also I'm not sure what to use for size and count arguments in fread.

    Here is the code so far.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define WRITE_FILE 	"rss.html"
    #define BEGIN_HTML 	"<html><head><title</title></head><body>"
    #define END_HTML 	"</body></html>"
    
    // global variables
    FILE* wFile;
    size_t wResult;
    struct item* items;
    
    void parse(char* content, size_t size, size_t count);
    char* getTagValue(char* string, const char* tag);
    
    
    struct item {
    	char* title;
    	char* description;
    	char* author;
    	char* link;
    	char* guid;
    	char* pubDate;
    };
    
    void parse(char* content, size_t size, size_t count) {
    	printf("Buffer addr: %p; length: %d\n", content, strlen(content));
    	void* wBuffer;
    	size_t newSize, newCount;
    	struct item inner;
    	char* tag;
    
    	printf("Size: %d", strlen(content));
    	printf("Description: %s\n", getTagValue(content, "description"));
    
    	
    	wResult = fwrite((void*)content, size, count, wFile);
    
    }
    
    char* getTagValue(char* string, const char* tag) {
    	char* ptr = strstr(string, tag);// causing invalid memory addresses
    	printf("check: ptr: %s", *ptr);
    		if(ptr != NULL) {
    			return ""; // actual code omitted
    		}
    }
    
    int main(int argc, char* argv[]) {
    	// variables for reading
    	FILE* rFile;
    	void* buffer;
    	size_t rSize, rCount, rResult;
    	
    	if(argc != 2) {
    		printf("WARNING: Wrong arguments\n");
    		return -1;	
    	}
    	rFile = fopen(argv[1], "rb");
    	if(rFile == NULL) {
    		printf("ERROR: File could not be openend\n");
    		return -1;
    	}
    	// Open file for writing in parse()
    	wFile = fopen(WRITE_FILE, "w");
    	
    	// Not sure what to use for rSize and rCount in fread()
    	rSize = 100;
    	rCount = 1;
    	buffer = malloc(rSize*rCount);
    	while(!feof(rFile)) {
    		rResult = fread(buffer, rSize, rCount, rFile);
    		parse((char*)buffer, rSize, rCount);
    	}
    	fclose(rFile);
    	fclose(wFile);
    	free(buffer);
    	return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The ptr that comes back from strstr can be NULL, as you (correctly) check for later. If you try to do *str when str is NULL, you deserve everything you get (which in this case is SIGSEGV).

    If your fread is just reading a text buffer, then you read as much as you can, limited either by how much is there or by how much your buffer can hold. fread will return how many items were read -- if it's maxed out, that means there could be more in the file; but if you tried to read 4096 characters and only 37 came in, that's a pretty good indicator that you're out of data.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    char* getTagValue(char* string, const char* tag) {
         char* ptr = strstr(string, tag);// causing invalid memory addresses
         if(ptr == NULL) 
              return NULL;
    
         printf("check: ptr: %s", *ptr);
         return ptr; // actual code omitted
    	}
    Tabstop has it right... you need to check if your pointer is null before you try printing the string. It's not causing an invalid address... it's not finding the tag.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    3
    I modified it and dereferenced ptr after the if-statement, but the error stays.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jessej89 View Post
    I modified it and dereferenced ptr after the if-statement, but the error stays.
    You don't need to dereference....

    Code:
         printf("check: ptr: %s", ptr);

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    3
    It's working, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with memory addresses
    By Fujy in forum C++ Programming
    Replies: 7
    Last Post: 01-18-2010, 09:31 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. clearing invalid value from memory
    By StringQuartet13 in forum C++ Programming
    Replies: 3
    Last Post: 09-29-2003, 10:19 PM
  4. Flood of errors when include .h
    By erik2004 in forum C++ Programming
    Replies: 14
    Last Post: 12-07-2002, 07:37 AM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 10:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21