Thread: Validating memory pointers

  1. #1
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103

    Validating memory pointers

    I saw a video today on validating memory pointers and thought it was interesting enough to post about it...

    The basic idea was that fopen checked that its passed filename was valid addressable memory(returned EFAULT if the address was invalid) and we could use that feature to validate memory addresses.

    Note: The video producer did say that this wasn't a recommended practice. He stated that its better to write correct code that doesn't rely on this feature.

    Code:
    #include <asm-generic/errno-base.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <string.h>
    
    int check_valid_address(void * ptr) {
      FILE * temp = fopen((char*)ptr, "r");
      if (temp) {
        fclose(temp);
      }else {
        if (errno == EFAULT) {
          return 1;
        }
      }
      return 0;
    }
    
    int main(int argc, char ** argv) {
      char * filename = NULL;
      int x = 4143;
      int * i_ptr = &x;
      void * f_ptr = check_valid_address;
      //Yeah! I know! Taking the address of a function and storing in a object pointer... Bad G4143
      check_valid_address(filename)
        ? fprintf(stdout, "Invalid address: %p\n", filename)
        : fprintf(stdout, "Valid address: %p\n", filename);
      check_valid_address(i_ptr)
        ? fprintf(stdout, "Invalid address: %p\n", (void*)i_ptr)
        : fprintf(stdout, "Valid address: %p\n", (void*)i_ptr);
      check_valid_address(f_ptr)
        ? fprintf(stdout, "Invalid address: %p\n", (void*)f_ptr)
        : fprintf(stdout, "Valid address: %p\n", (void*)f_ptr);
      return EXIT_SUCCESS;
    }
    Invalid address: (nil)
    Valid address: 0x7ffebd1c298c
    Valid address: 0x55bd8e0d71c9
    I always wondered why C never offered this feature in their standard library,

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I always wondered why C never offered this feature in their standard library,
    Because there is no great demand for such a thing.

    Like the other old favourite, getting the size of a malloc block.
    It's not needed as a library function because it's easy to track yourself if it's important to you.

    Because there is no portable way to do such a thing.
    The idea of a "valid address" is a function of both the hardware and your OS (if you even have an OS).


    Also, the code doesn't work for a couple of reasons.
    1. One of the first things the open call is going to do is scan from the given address looking for the \0 marking the end of the string.
    The given address may be a valid address, but it fails to find a \0 before running out of memory.

    2. If you happened to be pointing at say the memory mapped I/O registers, the simple act of just reading a location can cause real changes in state to the machine.
    Not so likely on a protected OS, but in a real-time / embedded system, it's a very real possibility.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Pointers
    By jdodle in forum C Programming
    Replies: 8
    Last Post: 04-27-2016, 12:09 AM
  2. Memory and pointers
    By tull2 in forum C Programming
    Replies: 2
    Last Post: 10-11-2010, 09:15 AM
  3. validating memory - minimum address of valid location
    By m37h0d in forum C++ Programming
    Replies: 12
    Last Post: 09-05-2008, 10:50 PM
  4. pointers and memory
    By itld in forum C++ Programming
    Replies: 4
    Last Post: 02-06-2002, 11:34 PM
  5. Memory to pointers
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-17-2001, 05:13 PM

Tags for this Thread