0

Since I don't really understand the overall issue I am having, it's become very difficult to debug.

char *o_key_pad = (char*)malloc(SHA256_DIGEST_LENGTH*sizeof(char));
for(int i = 0; i < SHA256_DIGEST_LENGTH; i++){
    o_key_pad[i] = 'a';
}
printf("%s\n", o_key_pad);

char *i_key_pad = (char*)malloc(SHA256_DIGEST_LENGTH*sizeof(char));
for(int i = 0; i < SHA256_DIGEST_LENGTH; i++){
    i_key_pad[i] = 'b';
}

printf("%s\n", o_key_pad);
printf("%s\n", i_key_pad);

And i obtain outputs:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb

Why does the array 'o_key_pad' get extended to include whatever i put in array 'i_key_pad', seems like some sort of memory issue?

Note: I understand that it can be done more effectively but to show my point more clearly I have laid it out like this.

Ross McQuillan
  • 75
  • 2
  • 11

3 Answers3

3

printf doesn't know where to stop unless you properly null-terminate your strings. C-style strings are as follows (the following two lines are equivalent; writing a string in quotes [a "string literal"] automatically creates a null-terminated character array):

char str[] = "hello world";
char str2[] = { 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '\0' };

This is what your code should look like:

int i;
char *o_key_pad = malloc(SHA256_DIGEST_LENGTH * sizeof (char) + 1);
for (i = 0; i < SHA256_DIGEST_LENGTH; ++i) {
    o_key_pad[i] = 'a';
}
o_key_pad[i] = '\0';
printf("%s\n", o_key_pad);

char *i_key_pad = malloc(SHA256_DIGEST_LENGTH * sizeof (char) + 1);
for (i = 0; i < SHA256_DIGEST_LENGTH; ++i) {
    i_key_pad[i] = 'b';
}
i_key_pad[i] = '\0';

printf("%s\n", o_key_pad);
printf("%s\n", i_key_pad);

/* Don't forget to free when done. */
free(o_key_pad);
free(i_key pad);
RastaJedi
  • 641
  • 1
  • 6
  • 18
  • Your code exhibits undefined behavior too. You write the null character beyond the end of the allocated space. – owacoder Mar 10 '16 at 18:17
  • Also note that technically `char *str3 = "hello world"; ` is slightly different than `str` and `str2` because in this case the array will not be modifiable, it will be placed in the read-only section. – RastaJedi Aug 11 '16 at 05:06
2

You have char arrays, but they do not have room for a string terminator, and in fact you do not append one. The contents are not, therefore, proper C strings. When you lie to printf() about that, anything can happen, but in practice, printing beyond the intended bounds of your data is a likely outcome.

If you do not want to add a terminator, then you could put a maximum field width into your format:

printf("%.32s\n", o_key_pad);

, or alternatively

printf("%.*s\n", SHA256_DIGEST_LENGTH, o_key_pad);

if you don't want to hardcode the field width directly into the format.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
1

Your strings are not NULL-terminated, resulting in undefined behavior when passed to printf. You could either allocate one more byte and add a NULL character, or specify a maximum field width for your string.

owacoder
  • 4,815
  • 20
  • 47