1

I am new to C programming and I am getting confused with the pointer math. I have an array of characters of size 32. It is my understanding that this means that the array is also 32 bytes since a character variable is 1 byte big therefore 32 characters * 1 byte = 32 bytes. The problem is when having a function that has a void pointer that is pointing to an array of characters as described before. I believe that the code segment

for (count = 0; count < size; count++)
*((int*) raw_sk + count) = 0

should set all of the slots in the raw_sk buffer should be set to 0. However, when I run the program, I get a segmentation fault. I thought that it could be possibly be the fact that I am adding count to the address. I thought that if I were to add one to an address I would be moving to the next slot in the array. Can someone please point out where I am going wrong? The function I am using is below. Thanks!

void
write_skfile (const char *skfname, void *raw_sk, size_t raw_sklen)
{
  int fdsk = 0;
  char *s = NULL;
  int status = 0;
  int count = 0;
  int size = (raw_sklen);


  /* armor the raw symmetric key in raw_sk using armor64 */
  s = armor64(raw_sk, raw_sklen);

  /* now let's write the armored symmetric key to skfname */

  if ((fdsk = open (skfname, O_WRONLY|O_TRUNC|O_CREAT, 0600)) == -1) {
    perror (getprogname ());

    /*scrubs the armored buffer*/
    for(count = 0; count < armor64len(s); count++)
    s[count] = '0';

    free (s);

    /* scrub the buffer that's holding the key before exiting */
   for (count = 0; count < size; count++)
    *((int*)raw_sk + count) = 0;

    exit (-1);
  }
  else {
    status = write (fdsk, s, strlen (s));
    if (status != -1) {
      status = write (fdsk, "\n", 1);
    }

   for (count = 0; (size_t)count < 22; count++)
    *((int*)raw_sk + count) = 0;

   free (s);
    close (fdsk);

    /* do not scrub the key buffer under normal circumstances
       (it's up to the caller) */ 

    if (status == -1) {
      printf ("%s: trouble writing symmetric key to file %s\n", 
          getprogname (), skfname);
      perror (getprogname ());

    /* scrub the buffer that's holding the key before exiting */

       /* scrub the buffer that's holding the key before exiting MY CODE
    for (count = 0; count < size; count++)
    *((int*)raw_sk + count) = 0;*/

      exit (-1);
    }
  }
}

3 Answers 3

3

You are incrementing the pointer by the size of an int. That is wrong. If you want to zero out the array you increment by the size of a char. Better yet, just use memset.

Sign up to request clarification or add additional context in comments.

2 Comments

so how could I increment by the size of a char? I thought that the compiler would do that for me, clearly I was mistaken. I will look into how to use memset.
@tpar44: You're casting raw_sk to an int*. Pointer arithmetic knows how to increment given the type of a pointer. for example, adding 1 to a char pointer increments the pointer by 1 byte. Adding 1 to an int pointer increments by 4 bytes(most of the time). We can generalize that and say that adding n to a pointer of type x increments the pointer by n * sizeof(x). So, if you had cast raw)_sk to a char* it would have been fine. Of course, the idiomatic way to do it would be to create a temporary pointer and simply increment that instead of adding a count variable.
0

Your loop iterates over size*sizeof(int) bytes in total (where most probably sizeof(int)==4), but the array is only size bytes large. Hence, segmentation fault.

Comments

0

I think you meant to do

*((char*) raw_sk + count) = 0

since I assume raw_sk is pointing to char array

pointer arithmatic works by moving the memory address by size of type so in this case you want char

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.