Skip to content

Commit

Permalink
Safe string functions refactor (#422)
Browse files Browse the repository at this point in the history
Port over from SVT-AV1
Author: John Stebbins <jstebbins@jetheaddev.com>

- Consolidate safe string functions in App
- Duplicate string functions in lib with different names
These functions are not desired in the lib's API, but are used in both
the lib and the App. They can't have the same name since this causes
duplicate symbols in statically link executable.

Signed-off-by: Jun Tian <jun.tian@intel.com>
  • Loading branch information
tianjunwork committed Nov 16, 2019
1 parent d0004f1 commit 58b3593
Show file tree
Hide file tree
Showing 9 changed files with 660 additions and 475 deletions.
3 changes: 2 additions & 1 deletion Source/App/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ include_directories(${PROJECT_SOURCE_DIR}/Source/API/
set(SvtHevcEncApp_Source
EbAppConfig.c
EbAppContext.c
EbAppFifo.c
EbAppString.c
EbAppMain.c
EbAppProcessCmd.c
EbTime.c
Expand All @@ -29,6 +29,7 @@ add_executable(SvtHevcEncApp
../API/EbApi.h
EbAppConfig.h
EbAppContext.h
EbAppString.h
../API/EbErrorCodes.h)

#********** SET COMPILE FLAGS************
Expand Down
32 changes: 1 addition & 31 deletions Source/App/EbAppConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <stdio.h>

#include "EbAppString.h"
#include "EbApi.h"

// Define Cross-Platform 64-bit fseek() and ftell()
Expand All @@ -20,16 +21,6 @@ typedef __int64 off64_t;
#define ftello64 ftell
#endif

#ifndef _RSIZE_T_DEFINED
typedef size_t rsize_t;
#define _RSIZE_T_DEFINED
#endif /* _RSIZE_T_DEFINED */

#ifndef _ERRNO_T_DEFINED
#define _ERRNO_T_DEFINED
typedef int32_t errno_t;
#endif /* _ERRNO_T_DEFINED */

/** The APPEXITCONDITIONTYPE type is used to define the App main loop exit
conditions.
*/
Expand Down Expand Up @@ -142,27 +133,6 @@ typedef struct EB_PARAM_PORTDEFINITIONTYPE {
} \
appMallocCount++;

/* string copy */
extern errno_t strcpy_ss(char *dest, rsize_t dmax, const char *src);

/* fitted string copy */
extern errno_t strncpy_ss(char *dest, rsize_t dmax, const char *src, rsize_t slen);

/* string length */
extern rsize_t strnlen_ss(const char *s, rsize_t smax);

#define EB_STRNCPY(dst, max_size, src, count) \
strncpy_ss(dst, max_size, src, count)

#define EB_STRCPY(dst, size, src) \
strcpy_ss(dst, size, src)

#define EB_STRCMP(target,token) \
strcmp(target,token)

#define EB_STRLEN(target, max_size) \
strnlen_ss(target, max_size)

#define EB_APP_MEMORY() \
printf("Total Number of Mallocs in App: %d\n", appMallocCount); \
printf("Total App Memory: %.2lf KB\n\n",*totalAppMemory/(double)1024);
Expand Down
1 change: 0 additions & 1 deletion Source/App/EbAppInputy4m.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#define CHROMA_MAX 4

#include "EbAppConfig.h"
#include <ctype.h>

/* copy a string until a specified character or a new line is found */
Expand Down
90 changes: 1 addition & 89 deletions Source/App/EbAppFifo.c → Source/App/EbAppString.c
Original file line number Diff line number Diff line change
@@ -1,92 +1,8 @@

#include "EbAppConfig.h"
#include "EbAppString.h"

/* SAFE STRING LIBRARY */

#ifndef EOK
#define EOK ( 0 )
#endif

#ifndef ESZEROL
#define ESZEROL ( 401 ) /* length is zero */
#endif

#ifndef ESLEMIN
#define ESLEMIN ( 402 ) /* length is below min */
#endif

#ifndef ESLEMAX
#define ESLEMAX ( 403 ) /* length exceeds max */
#endif

#ifndef ESNULLP
#define ESNULLP ( 400 ) /* null ptr */
#endif

#ifndef ESOVRLP
#define ESOVRLP ( 404 ) /* overlap undefined */
#endif

#ifndef ESEMPTY
#define ESEMPTY ( 405 ) /* empty string */
#endif

#ifndef ESNOSPC
#define ESNOSPC ( 406 ) /* not enough space for s2 */
#endif

#ifndef ESUNTERM
#define ESUNTERM ( 407 ) /* unterminated string */
#endif

#ifndef ESNODIFF
#define ESNODIFF ( 408 ) /* no difference */
#endif

#ifndef ESNOTFND
#define ESNOTFND ( 409 ) /* not found */
#endif

#define RSIZE_MAX_MEM ( 256UL << 20 ) /* 256MB */

#define RCNEGATE(x) (x)
#define RSIZE_MAX_STR ( 4UL << 10 ) /* 4KB */
#define sl_default_handler ignore_handler_s
#define EXPORT_SYMBOL(sym)

#ifndef sldebug_printf
#define sldebug_printf(...)
#endif

/*
* Function used by the libraries to invoke the registered
* runtime-constraint handler. Always needed.
*/

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

/*
* Function used by the libraries to invoke the registered
* runtime-constraint handler. Always needed.
*/
extern void invoke_safe_str_constraint_handler(
const char *msg,
void *ptr,
errno_t error);


static inline void handle_error(char *orig_dest, rsize_t orig_dmax,
char *err_msg, errno_t err_code)
{
(void)orig_dmax;
*orig_dest = '\0';

invoke_safe_str_constraint_handler(err_msg, NULL, err_code);
return;
}
static constraint_handler_t str_handler = NULL;

void
Expand All @@ -111,7 +27,6 @@ void ignore_handler_s(const char *msg, void *ptr, errno_t error)
(msg) ? msg : "Null message");
return;
}
EXPORT_SYMBOL(ignore_handler_s)

errno_t
strncpy_ss(char *dest, rsize_t dmax, const char *src, rsize_t slen)
Expand Down Expand Up @@ -236,7 +151,6 @@ strncpy_ss(char *dest, rsize_t dmax, const char *src, rsize_t slen)
ESNOSPC);
return RCNEGATE(ESNOSPC);
}
EXPORT_SYMBOL(strncpy_ss)

errno_t
strcpy_ss(char *dest, rsize_t dmax, const char *src)
Expand Down Expand Up @@ -331,7 +245,6 @@ strcpy_ss(char *dest, rsize_t dmax, const char *src)
ESNOSPC);
return RCNEGATE(ESNOSPC);
}
EXPORT_SYMBOL(strcpy_ss)

rsize_t
strnlen_ss(const char *dest, rsize_t dmax)
Expand Down Expand Up @@ -363,6 +276,5 @@ strnlen_ss(const char *dest, rsize_t dmax)

return RCNEGATE(count);
}
EXPORT_SYMBOL(strnlen_ss)

/* SAFE STRING LIBRARY */
172 changes: 172 additions & 0 deletions Source/App/EbAppString.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/*------------------------------------------------------------------
* strncpy_s.c / strcpy_s.c / strnlen_s.c
*
* October 2008, Bo Berry
*
* Copyright � 2008-2011 by Cisco Systems, Inc
* All rights reserved.
* safe_str_constraint.c
*
* October 2008, Bo Berry
* 2012, Jonathan Toppins <jtoppins@users.sourceforge.net>
*
* Copyright � 2008, 2009, 2012 Cisco Systems
* All rights reserved.
* ignore_handler_s.c
*
* 2012, Jonathan Toppins <jtoppins@users.sourceforge.net>
*
* Copyright � 2012 Cisco Systems
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------------
*/
#ifndef EbAppString_h
#define EbAppString_h

#include <stddef.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

#ifndef _RSIZE_T_DEFINED
typedef size_t rsize_t;
#define _RSIZE_T_DEFINED
#endif /* _RSIZE_T_DEFINED */

#ifndef _ERRNO_T_DEFINED
#define _ERRNO_T_DEFINED
typedef int32_t errno_t;
#endif /* _ERRNO_T_DEFINED */

#ifndef EOK
#define EOK ( 0 )
#endif

#ifndef ESZEROL
#define ESZEROL ( 401 ) /* length is zero */
#endif

#ifndef ESLEMIN
#define ESLEMIN ( 402 ) /* length is below min */
#endif

#ifndef ESLEMAX
#define ESLEMAX ( 403 ) /* length exceeds max */
#endif

#ifndef ESNULLP
#define ESNULLP ( 400 ) /* null ptr */
#endif

#ifndef ESOVRLP
#define ESOVRLP ( 404 ) /* overlap undefined */
#endif

#ifndef ESEMPTY
#define ESEMPTY ( 405 ) /* empty string */
#endif

#ifndef ESNOSPC
#define ESNOSPC ( 406 ) /* not enough space for s2 */
#endif

#ifndef ESUNTERM
#define ESUNTERM ( 407 ) /* unterminated string */
#endif

#ifndef ESNODIFF
#define ESNODIFF ( 408 ) /* no difference */
#endif

#ifndef ESNOTFND
#define ESNOTFND ( 409 ) /* not found */
#endif

#define RSIZE_MAX_STR ( 4UL << 10 ) /* 4KB */
#define RCNEGATE(x) (x)

#ifndef sldebug_printf
#define sldebug_printf(...)
#endif

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

/*
* Function used by the libraries to invoke the registered
* runtime-constraint handler. Always needed.
*/
extern void invoke_safe_str_constraint_handler(
const char *msg,
void *ptr,
errno_t error);

static inline void handle_error(char *orig_dest, rsize_t orig_dmax,
char *err_msg, errno_t err_code)
{
(void)orig_dmax;
*orig_dest = '\0';

invoke_safe_str_constraint_handler(err_msg, NULL, err_code);
return;
}

#define sl_default_handler ignore_handler_s
extern void ignore_handler_s(const char *msg, void *ptr, errno_t error);

/* string copy */
errno_t strcpy_ss(
char *dest, rsize_t dmax, const char *src);

/* fitted string copy */
errno_t strncpy_ss(
char *dest, rsize_t dmax, const char *src, rsize_t slen);

/* string length */
rsize_t strnlen_ss(
const char *s, rsize_t smax);

#define EB_STRNCPY(dst, dst_size, src, count) \
strncpy_ss(dst, dst_size, src, count)

#define EB_STRCPY(dst, size, src) \
strcpy_ss(dst, size, src)

#define EB_STRCMP(target,token) \
strcmp(target,token)

#define EB_STRLEN(target, max_size) \
strnlen_ss(target, max_size)

#ifdef __cplusplus
}
#endif // __cplusplus

#endif // EbAppString_h
/* File EOF */
2 changes: 2 additions & 0 deletions Source/Lib/Codec/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ set(Codec_Source
EbSei.c
EbTransforms.c
EbTransQuantBuffers.c
EbString.c
EbUtility.c)

if(COMPILE_AS_CPP)
Expand Down Expand Up @@ -187,6 +188,7 @@ add_library(SvtHevcEnc
EbTransforms.h
EbTransformUnit.h
EbTransQuantBuffers.h
EbString.h
EbUtility.h)

if(UNIX)
Expand Down
Loading

0 comments on commit 58b3593

Please sign in to comment.