Skip to content

Commit

Permalink
eliminate str/mem/lib inclusion loops
Browse files Browse the repository at this point in the history
you need to include the right header file now,
either safe_str_lib.h, safe_mem_lib.h or safe_lib.h for the rest.
Similar behaviour as before, though a bit complex.

Closes GH #14
  • Loading branch information
rurban committed Oct 2, 2017
1 parent 1b5b87d commit 9be3723
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 26 deletions.
20 changes: 3 additions & 17 deletions include/safe_lib.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/*------------------------------------------------------------------
* safe_lib.h -- Safe C Library
*
* October 2008, Bo Berry
* September 2017, Reini Urban
* Modified 2012, Jonathan Toppins <jtoppins@users.sourceforge.net>
*
* Copyright (c) 2008-2013 by Cisco Systems, Inc
* Copyright (c) 2017 by Reini Urban
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
Expand Down Expand Up @@ -38,8 +39,8 @@ extern "C" {
#endif

#include "safe_config.h"
#include "safe_types.h"
#include "safe_lib_errno.h"
#include "safe_types.h"

#include <time.h>
#if defined HAVE_SYS_TIME_H
Expand All @@ -57,17 +58,6 @@ extern "C" {
# define EXTERN extern
#endif

/* C11 appendix K types - specific for bounds checking */
typedef size_t rsize_t;

#ifndef RSIZE_MAX
# define RSIZE_MAX (~(rsize_t)0) /* leave here for completeness */
#endif

typedef void (*constraint_handler_t) (const char *restrict /* msg */,
void *restrict /* ptr */,
errno_t /* error */);

EXTERN void
abort_handler_s(const char *restrict msg, void *restrict ptr, errno_t error);

Expand All @@ -76,10 +66,6 @@ ignore_handler_s(const char *restrict msg, void *restrict ptr, errno_t error);

#define sl_default_handler ignore_handler_s

#include "safe_mem_lib.h"
#include "safe_str_lib.h"


#ifndef TMP_MAX_S
# ifdef TMP_MAX
# define TMP_MAX_S TMP_MAX
Expand Down
15 changes: 14 additions & 1 deletion include/safe_mem_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,25 @@
extern "C" {
#endif

#include "safe_lib.h"
#include "safe_config.h"
#include "safe_lib_errno.h"
#include "safe_types.h"

#ifndef SAFECLIB_DISABLE_WCHAR
#include <wchar.h>
#endif

/* we disable static builds on mingw for now */
#if defined _WIN32 /* && defined DLL_EXPORT */
# if defined(EXPORT) && defined(__SAFECLIB_PRIVATE_H__)
# define EXTERN extern __declspec(dllexport)
# else
# define EXTERN extern __declspec(dllimport)
# endif
#else
# define EXTERN extern
#endif

#define RSIZE_MAX_MEM16 ( RSIZE_MAX_MEM/2 )
#define RSIZE_MAX_MEM32 ( RSIZE_MAX_MEM/4 )
#define RSIZE_MAX_WMEM ( RSIZE_MAX_MEM/sizeof(wchar_t) )
Expand Down
26 changes: 25 additions & 1 deletion include/safe_str_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,30 @@
extern "C" {
#endif

#include "safe_lib.h"
#include "safe_config.h"
#include "safe_lib_errno.h"
#include "safe_types.h"

#include <stdarg.h>
#include <time.h>
#if defined HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifndef SAFECLIB_DISABLE_WCHAR
#include <wchar.h>
#endif

/* we disable static builds on mingw for now */
#if defined _WIN32 /* && defined DLL_EXPORT */
# if defined(EXPORT) && defined(__SAFECLIB_PRIVATE_H__)
# define EXTERN extern __declspec(dllexport)
# else
# define EXTERN extern __declspec(dllimport)
# endif
#else
# define EXTERN extern
#endif

/*
* The shortest string is a null string!!
*/
Expand All @@ -62,6 +79,13 @@ extern "C" {
#define SAFE_STR_PASSWORD_MIN_LENGTH ( 6 )
#define SAFE_STR_PASSWORD_MAX_LENGTH ( 32 )

EXTERN void
abort_handler_s(const char *restrict msg, void *restrict ptr, errno_t error);

EXTERN void
ignore_handler_s(const char *restrict msg, void *restrict ptr, errno_t error);

#define sl_default_handler ignore_handler_s

/* set string constraint handler */
EXTERN constraint_handler_t
Expand Down
11 changes: 11 additions & 0 deletions include/safe_types.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
extern "C" {
#endif

/* C11 appendix K types - specific for bounds checking */
typedef size_t rsize_t;

#ifndef RSIZE_MAX
# define RSIZE_MAX (~(rsize_t)0) /* leave here for completeness */
#endif

#ifdef __KERNEL__
/* linux kernel environment */

Expand All @@ -63,6 +70,10 @@ typedef int errno_t;

#endif /* __KERNEL__ */

typedef void (*constraint_handler_t) (const char *restrict /* msg */,
void *restrict /* ptr */,
errno_t /* error */);

#ifdef __cplusplus
}
#endif
Expand Down
5 changes: 4 additions & 1 deletion src/safeclib_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,11 @@ void abort(void) __attribute__((noreturn));
#define EXPORT
#endif

#include "safe_lib.h"
#include "safe_str_lib.h"
#include "safe_str_constraint.h"
#include "safe_mem_lib.h"
#include "safe_mem_constraint.h"
#include "safe_lib.h"

/* platform quirks */
#ifndef SAFECLIB_DISABLE_WCHAR
Expand Down
3 changes: 1 addition & 2 deletions src/str/safe_str_constraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@
* Function used by the libraries to invoke the registered
* runtime-constraint handler. Always needed.
*/
extern void invoke_safe_str_constraint_handler(
EXTERN void invoke_safe_str_constraint_handler(
const char *msg,
void *ptr,
errno_t error);


/*
* Safe C Lib internal string routine to consolidate error handling.
* With SAFECLIB_STR_NULL_SLACK clear the dest buffer to eliminate
Expand Down
2 changes: 1 addition & 1 deletion tests/test_memccpy_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

#include "test_private.h"
#include "safe_str_lib.h"
#include "safe_mem_lib.h"

#define LEN ( 128 )

Expand Down
2 changes: 1 addition & 1 deletion tests/test_towfc_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/

#include "test_private.h"
#include "safe_lib.h"
#include "safe_str_lib.h"
#include <stdlib.h>
#include <unistd.h>

Expand Down
2 changes: 1 addition & 1 deletion tests/test_towlower.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/

#include "test_private.h"
#include "safe_lib.h"
#include "safe_str_lib.h"
#include <stdlib.h>
#include <ctype.h>

Expand Down
2 changes: 1 addition & 1 deletion tests/test_towupper.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

#include "test_private.h"
#include "safe_lib.h"
#include "safe_str_lib.h"
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
Expand Down

0 comments on commit 9be3723

Please sign in to comment.