_debug_strnset — Set characters in a string

Format

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

Purpose

This is the debug version of strnset. Like strnset, it sets, at most, the first n characters of string to c (converted to a char), where if n is greater than the length of string, the length of string is used in place of n.

_debug_strnset validates the heap after setting the bytes, and performs this check only when the target is within a heap. _debug_strnset makes an implicit call to _heap_check. If _debug_strnset detects a corrupted heap when it makes a call to _heap_check, _debug_strnset 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 two programming errors. The string, str, was created without a null-terminator to mark the end of the string, and without the terminator strnset with a count of 10 stores bytes past the end of the allocated object.

/*  _debug_strnset   */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
   char *str;
   str = (char*)malloc(10);
   printf("This is the string after strnset: %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: 7878787878797979.
This memory block was (re)allocated at line number 9 in _debug_strnset.c.
Heap state was valid at line 11 of _debug_strnset.c.