Last active
August 29, 2015 14:07
-
-
Save jbenner-radham/230c199fdfce03e3844a to your computer and use it in GitHub Desktop.
Experimenting with libsafec for C11 safe string function goodness.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> /* printf(), fprintf() */ | |
#include <stdbool.h> | |
#include <stdint.h> | |
#include <stdlib.h> /* atoi() */ | |
#include <string.h> /* memset(), strlen() */ | |
#include <unistd.h> /* close() */ | |
#include "safe_lib.h" | |
/** | |
* cc -std=c11 -Wall libsafec-test.c /usr/local/lib/libsafec-1.0.a -I/usr/local/include/libsafec | |
*/ | |
/** | |
* typedef void (*safe_lib_constraint_handler_t)(const char *msg, void *ptr,errno_t error); | |
* | |
* safe_lib_constraint_handler_t safe_lib_set_constraint_handler(safe_lib_constraint_handler_t handler) | |
*/ | |
/** | |
* errno_t strcat_s(char *dest, rsize_t dmax, const char *src); | |
* | |
* // Alternative naming - conforming to the error codes | |
* errno_t strcat_s(char *s2, rsize_t dmax, const char *s1); | |
*/ | |
/** | |
* The following are defined in `libsafec/include/safe_lib_errno.h` | |
*/ | |
/// #define EOK ( 0 ) | |
/// #define ESNULLP ( 400 ) /* null ptr */ | |
/// #define ESZEROL ( 401 ) /* length is zero */ | |
/// #define ESLEMAX ( 403 ) /* length exceeds max */ | |
/// #define ESOVRLP ( 404 ) /* overlap undefined */ | |
/// #define ESNOSPC ( 406 ) /* not enough space for s2 */ | |
/// #define ESUNTERM ( 407 ) /* unterminated string */ | |
/// #define ESNOTFND ( 409 ) /* not found */ | |
/** | |
* ESNOSPC: "Not enough space for s2" | |
* | |
* If the `rsize_t dmax` isn't large enough this will be returned. | |
*/ | |
void strcat_s_exit_on_err(errno_t code); | |
int main(void) | |
{ | |
errno_t code; | |
char *dest = malloc(BUFSIZ); | |
char *src = "Hello"; | |
rsize_t limit = BUFSIZ - 1; | |
/// printf("BUFSIZ: %d\n", BUFSIZ); | |
/// printf("RSIZE_MAX_STR: %lu\n", RSIZE_MAX_STR); // <-- 4096 | |
/// printf("LIMIT: %zu\n", limit); | |
code = strcat_s(dest, limit, src); | |
strcat_s_exit_on_err(code); | |
printf("[%lu] => \"%s\"\n", strlen(dest), dest); | |
// ----------- | |
char* src2 = ", "; | |
code = strcat_s(dest, limit, src2); | |
strcat_s_exit_on_err(code); | |
printf("[%lu] => \"%s\"\n", strlen(dest), dest); | |
// ----------- | |
char* src3 = "world."; | |
code = strcat_s(dest, limit, src3); | |
strcat_s_exit_on_err(code); | |
printf("[%lu] => \"%s\"\n", strlen(dest), dest); | |
// ------- | |
// CLEANUP | |
// ------- | |
free(dest); | |
} | |
void strcat_s_exit_on_err(errno_t code) | |
{ | |
if (code == EOK) { | |
return; | |
} | |
fprintf(stderr, "strcat_s() encountered an error: "); | |
switch(code) | |
{ | |
case ESNULLP: | |
fprintf(stderr, "Null pointer\n"); | |
break; | |
case ESZEROL: | |
fprintf(stderr, "Length is zero\n"); | |
break; | |
case ESLEMAX: | |
fprintf(stderr, "Length exceeds max\n"); | |
break; | |
case ESOVRLP: | |
fprintf(stderr, "Overlap undefined\n"); | |
break; | |
case ESNOSPC: | |
fprintf(stderr, "Not enough space for s2\n"); | |
break; | |
case ESUNTERM: | |
fprintf(stderr, "Unterminated string\n"); | |
break; | |
case ESNOTFND: | |
fprintf(stderr, "Not found\n"); | |
break; | |
default: | |
fprintf(stderr, "Unknown error code\n"); | |
break; | |
} | |
exit(code); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment