_debug_strset — Set characters in a string

Format

#include <string.h>
char *_debug_strset(char *string, size_t c, const char *file, size_t line);

Purpose

This is the debug version of strset. Like strset, it sets all characters of string, except the ending null character (\0), to c (converted to a char).

_debug_strset validates the heap after setting all characters of string, and performs this check only when the target is within a heap. _debug_strset makes an implicit call to _heap_check. If _debug_strset detects a corrupted heap when it makes a call to _heap_check, _debug_strset reports the file name file and line number line in a message.

Return values

Returns a pointer to the altered string. There is no error return value.

Examples

This example contains a programming error. The string, str, was created without a null-terminator, and strset propagates the letter 'k' until it finds what it thinks is the null-terminator.

/*  file: _debug_strset.c  */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
   char *str;
   str = (char*)malloc(10);
   strnset(str, 'x', 5);
   strset(str+5, 'k');
   printf("This is the string after strset: %s\n", str);
   return 0;
}

The output is similar to:

End of allocated object 0x00073c80 was overwritten at 0x00073c8a.
The first eight bytes of the memory block (in hex) are: 78787878786B6B6B.
This memory block was (re)allocated at line number 9 in _debug_strset.c.
Heap state was valid at line 11 of _debug_strset.c.
Memory error detected at line 12 of _debug_strset.c.