[Forensics-changes] [yara] 130/135: Provide an implementation for strlcat and strlcpy and use them where appropriate
Hilko Bengen
bengen at moszumanska.debian.org
Sat Jul 1 10:27:40 UTC 2017
This is an automated email from the git hooks/post-receive script.
bengen pushed a commit to annotated tag v3.1.0
in repository yara.
commit d196b97bb7621822f5209d8f08461f1bdb368786
Author: Victor M. Alvarez <plusvic at gmail.com>
Date: Mon Aug 25 15:57:32 2014 +0200
Provide an implementation for strlcat and strlcpy and use them where appropriate
---
libyara/hex_lexer.c | 13 ++++---
libyara/hex_lexer.l | 4 +--
libyara/include/yara/compiler.h | 4 +--
libyara/include/yara/utils.h | 8 +++++
libyara/lexer.c | 73 ++++++++++++++++++--------------------
libyara/lexer.l | 6 ++--
libyara/modules/pe.c | 3 +-
libyara/re_lexer.c | 13 ++++---
libyara/re_lexer.l | 4 +--
libyara/utils.c | 78 +++++++++++++++++++++++++++++++++++++++++
10 files changed, 141 insertions(+), 65 deletions(-)
diff --git a/libyara/hex_lexer.c b/libyara/hex_lexer.c
index b41a313..eb64437 100644
--- a/libyara/hex_lexer.c
+++ b/libyara/hex_lexer.c
@@ -47,7 +47,6 @@ typedef int16_t flex_int16_t;
typedef uint16_t flex_uint16_t;
typedef int32_t flex_int32_t;
typedef uint32_t flex_uint32_t;
-typedef uint64_t flex_uint64_t;
#else
typedef signed char flex_int8_t;
typedef short int flex_int16_t;
@@ -358,7 +357,7 @@ static void yy_fatal_error (yyconst char msg[] ,yyscan_t yyscanner );
*/
#define YY_DO_BEFORE_ACTION \
yyg->yytext_ptr = yy_bp; \
- yyleng = (yy_size_t) (yy_cp - yy_bp); \
+ yyleng = (size_t) (yy_cp - yy_bp); \
yyg->yy_hold_char = *yy_cp; \
*yy_cp = '\0'; \
yyg->yy_c_buf_p = yy_cp;
@@ -504,7 +503,7 @@ limitations under the License.
#define YY_NO_UNISTD_H 1
#define YY_NO_INPUT 1
-#line 508 "hex_lexer.c"
+#line 507 "hex_lexer.c"
#define INITIAL 0
#define range 1
@@ -740,7 +739,7 @@ YY_DECL
-#line 744 "hex_lexer.c"
+#line 743 "hex_lexer.c"
yylval = yylval_param;
@@ -943,7 +942,7 @@ YY_RULE_SETUP
#line 138 "hex_lexer.l"
ECHO;
YY_BREAK
-#line 947 "hex_lexer.c"
+#line 946 "hex_lexer.c"
case YY_STATE_EOF(INITIAL):
case YY_STATE_EOF(range):
yyterminate();
@@ -2122,7 +2121,7 @@ void yyerror(
{
lex_env->last_error_code = ERROR_INVALID_HEX_STRING;
- strncpy(
+ strlcpy(
lex_env->last_error_message,
error_message,
sizeof(lex_env->last_error_message));
@@ -2176,7 +2175,7 @@ int yr_parse_hex_string(
if (lex_env.last_error_code != ERROR_SUCCESS)
{
- strncpy(error->message, lex_env.last_error_message, sizeof(error->message));
+ strlcpy(error->message, lex_env.last_error_message, sizeof(error->message));
return lex_env.last_error_code;
}
diff --git a/libyara/hex_lexer.l b/libyara/hex_lexer.l
index 50f8bbb..81f8573 100644
--- a/libyara/hex_lexer.l
+++ b/libyara/hex_lexer.l
@@ -176,7 +176,7 @@ void yyerror(
{
lex_env->last_error_code = ERROR_INVALID_HEX_STRING;
- strncpy(
+ strlcpy(
lex_env->last_error_message,
error_message,
sizeof(lex_env->last_error_message));
@@ -230,7 +230,7 @@ int yr_parse_hex_string(
if (lex_env.last_error_code != ERROR_SUCCESS)
{
- strncpy(error->message, lex_env.last_error_message, sizeof(error->message));
+ strlcpy(error->message, lex_env.last_error_message, sizeof(error->message));
return lex_env.last_error_code;
}
diff --git a/libyara/include/yara/compiler.h b/libyara/include/yara/compiler.h
index 3b64a9d..fa5115e 100644
--- a/libyara/include/yara/compiler.h
+++ b/libyara/include/yara/compiler.h
@@ -93,12 +93,10 @@ typedef struct _YR_COMPILER
#define yr_compiler_set_error_extra_info(compiler, info) \
- strncpy( \
+ strlcpy( \
compiler->last_error_extra_info, \
info, \
sizeof(compiler->last_error_extra_info)); \
- compiler->last_error_extra_info[ \
- sizeof(compiler->last_error_extra_info) - 1] = 0;
int _yr_compiler_push_file(
diff --git a/libyara/include/yara/utils.h b/libyara/include/yara/utils.h
index 3cdc700..c793e68 100644
--- a/libyara/include/yara/utils.h
+++ b/libyara/include/yara/utils.h
@@ -66,4 +66,12 @@ limitations under the License.
size_t xtoi(const char* hexstr);
+#if !defined(__FreeBSD__) && !defined(__OpenBSD__)
+
+size_t strlcpy(char *dst, const char *src, size_t size);
+
+size_t strlcat(char *dst, const char *src, size_t size);
+
+#endif
+
#endif
diff --git a/libyara/lexer.c b/libyara/lexer.c
index 44e7941..3ec4d57 100644
--- a/libyara/lexer.c
+++ b/libyara/lexer.c
@@ -47,7 +47,6 @@ typedef int16_t flex_int16_t;
typedef uint16_t flex_uint16_t;
typedef int32_t flex_int32_t;
typedef uint32_t flex_uint32_t;
-typedef uint64_t flex_uint64_t;
#else
typedef signed char flex_int8_t;
typedef short int flex_int16_t;
@@ -358,7 +357,7 @@ static void yy_fatal_error (yyconst char msg[] ,yyscan_t yyscanner );
*/
#define YY_DO_BEFORE_ACTION \
yyg->yytext_ptr = yy_bp; \
- yyleng = (yy_size_t) (yy_cp - yy_bp); \
+ yyleng = (size_t) (yy_cp - yy_bp); \
yyg->yy_hold_char = *yy_cp; \
*yy_cp = '\0'; \
yyg->yy_c_buf_p = yy_cp;
@@ -681,7 +680,7 @@ limitations under the License.
-#line 685 "lexer.c"
+#line 684 "lexer.c"
#define INITIAL 0
#define str 1
@@ -919,7 +918,7 @@ YY_DECL
#line 83 "lexer.l"
-#line 923 "lexer.c"
+#line 922 "lexer.c"
yylval = yylval_param;
@@ -1278,8 +1277,7 @@ YY_RULE_SETUP
if (current_file_name != NULL)
{
- strncpy(buffer, current_file_name, sizeof(buffer)-1);
- buffer[sizeof(buffer)-1] = '\0';
+ strlcpy(buffer, current_file_name, sizeof(buffer));
}
else
{
@@ -1297,8 +1295,7 @@ YY_RULE_SETUP
{
f = (b > s)? (b + 1): (s + 1);
- strncpy(f, yyextra->lex_buf, sizeof(buffer) - (f - buffer));
- buffer[sizeof(buffer)-1] = '\0';
+ strlcpy(f, yyextra->lex_buf, sizeof(buffer) - (f - buffer));
f = buffer;
@@ -1367,7 +1364,7 @@ case YY_STATE_EOF(str):
case YY_STATE_EOF(regexp):
case YY_STATE_EOF(include):
case YY_STATE_EOF(comment):
-#line 250 "lexer.l"
+#line 248 "lexer.l"
{
YR_COMPILER* compiler = yara_yyget_extra(yyscanner);
@@ -1389,7 +1386,7 @@ case YY_STATE_EOF(comment):
YY_BREAK
case 49:
YY_RULE_SETUP
-#line 270 "lexer.l"
+#line 268 "lexer.l"
{
yylval->c_string = yr_strdup(yytext);
@@ -1405,7 +1402,7 @@ YY_RULE_SETUP
YY_BREAK
case 50:
YY_RULE_SETUP
-#line 284 "lexer.l"
+#line 282 "lexer.l"
{
yylval->c_string = yr_strdup(yytext);
@@ -1421,7 +1418,7 @@ YY_RULE_SETUP
YY_BREAK
case 51:
YY_RULE_SETUP
-#line 298 "lexer.l"
+#line 296 "lexer.l"
{
yylval->c_string = yr_strdup(yytext);
@@ -1438,7 +1435,7 @@ YY_RULE_SETUP
YY_BREAK
case 52:
YY_RULE_SETUP
-#line 313 "lexer.l"
+#line 311 "lexer.l"
{
yylval->c_string = yr_strdup(yytext);
@@ -1455,7 +1452,7 @@ YY_RULE_SETUP
YY_BREAK
case 53:
YY_RULE_SETUP
-#line 328 "lexer.l"
+#line 326 "lexer.l"
{
if (strlen(yytext) > 128)
@@ -1476,7 +1473,7 @@ YY_RULE_SETUP
YY_BREAK
case 54:
YY_RULE_SETUP
-#line 347 "lexer.l"
+#line 345 "lexer.l"
{
yylval->integer = (size_t) atol(yytext);
@@ -1494,7 +1491,7 @@ YY_RULE_SETUP
YY_BREAK
case 55:
YY_RULE_SETUP
-#line 363 "lexer.l"
+#line 361 "lexer.l"
{
yylval->integer = xtoi(yytext + 2);
@@ -1503,7 +1500,7 @@ YY_RULE_SETUP
YY_BREAK
case 56:
YY_RULE_SETUP
-#line 370 "lexer.l"
+#line 368 "lexer.l"
{ /* saw closing quote - all done */
SIZED_STRING* s;
@@ -1529,7 +1526,7 @@ YY_RULE_SETUP
YY_BREAK
case 57:
YY_RULE_SETUP
-#line 394 "lexer.l"
+#line 392 "lexer.l"
{
LEX_CHECK_SPACE_OK("\t", yyextra->lex_buf_len, LEX_BUF_SIZE);
@@ -1539,7 +1536,7 @@ YY_RULE_SETUP
YY_BREAK
case 58:
YY_RULE_SETUP
-#line 402 "lexer.l"
+#line 400 "lexer.l"
{
LEX_CHECK_SPACE_OK("\n", yyextra->lex_buf_len, LEX_BUF_SIZE);
@@ -1549,7 +1546,7 @@ YY_RULE_SETUP
YY_BREAK
case 59:
YY_RULE_SETUP
-#line 410 "lexer.l"
+#line 408 "lexer.l"
{
LEX_CHECK_SPACE_OK("\"", yyextra->lex_buf_len, LEX_BUF_SIZE);
@@ -1559,7 +1556,7 @@ YY_RULE_SETUP
YY_BREAK
case 60:
YY_RULE_SETUP
-#line 418 "lexer.l"
+#line 416 "lexer.l"
{
LEX_CHECK_SPACE_OK("\\", yyextra->lex_buf_len, LEX_BUF_SIZE);
@@ -1569,7 +1566,7 @@ YY_RULE_SETUP
YY_BREAK
case 61:
YY_RULE_SETUP
-#line 426 "lexer.l"
+#line 424 "lexer.l"
{
int result;
@@ -1582,13 +1579,13 @@ YY_RULE_SETUP
YY_BREAK
case 62:
YY_RULE_SETUP
-#line 437 "lexer.l"
+#line 435 "lexer.l"
{ YYTEXT_TO_BUFFER; }
YY_BREAK
case 63:
/* rule 63 can match eol */
YY_RULE_SETUP
-#line 440 "lexer.l"
+#line 438 "lexer.l"
{
yyerror(yyscanner, compiler, "unterminated string");
@@ -1598,7 +1595,7 @@ YY_RULE_SETUP
case 64:
/* rule 64 can match eol */
YY_RULE_SETUP
-#line 446 "lexer.l"
+#line 444 "lexer.l"
{
yyerror(yyscanner, compiler, "illegal escape sequence");
@@ -1606,7 +1603,7 @@ YY_RULE_SETUP
YY_BREAK
case 65:
YY_RULE_SETUP
-#line 452 "lexer.l"
+#line 450 "lexer.l"
{
SIZED_STRING* s;
@@ -1639,7 +1636,7 @@ YY_RULE_SETUP
YY_BREAK
case 66:
YY_RULE_SETUP
-#line 483 "lexer.l"
+#line 481 "lexer.l"
{
LEX_CHECK_SPACE_OK("/", yyextra->lex_buf_len, LEX_BUF_SIZE);
@@ -1649,7 +1646,7 @@ YY_RULE_SETUP
YY_BREAK
case 67:
YY_RULE_SETUP
-#line 491 "lexer.l"
+#line 489 "lexer.l"
{
LEX_CHECK_SPACE_OK("\\.", yyextra->lex_buf_len, LEX_BUF_SIZE);
@@ -1660,13 +1657,13 @@ YY_RULE_SETUP
YY_BREAK
case 68:
YY_RULE_SETUP
-#line 500 "lexer.l"
+#line 498 "lexer.l"
{ YYTEXT_TO_BUFFER; }
YY_BREAK
case 69:
/* rule 69 can match eol */
YY_RULE_SETUP
-#line 503 "lexer.l"
+#line 501 "lexer.l"
{
yyerror(yyscanner, compiler, "unterminated regular expression");
@@ -1675,7 +1672,7 @@ YY_RULE_SETUP
YY_BREAK
case 70:
YY_RULE_SETUP
-#line 510 "lexer.l"
+#line 508 "lexer.l"
{
yyextra->lex_buf_ptr = yyextra->lex_buf;
@@ -1685,7 +1682,7 @@ YY_RULE_SETUP
YY_BREAK
case 71:
YY_RULE_SETUP
-#line 518 "lexer.l"
+#line 516 "lexer.l"
{
yyextra->lex_buf_ptr = yyextra->lex_buf;
@@ -1696,7 +1693,7 @@ YY_RULE_SETUP
case 72:
/* rule 72 can match eol */
YY_RULE_SETUP
-#line 526 "lexer.l"
+#line 524 "lexer.l"
{
int len = strlen(yytext);
@@ -1714,12 +1711,12 @@ YY_RULE_SETUP
case 73:
/* rule 73 can match eol */
YY_RULE_SETUP
-#line 541 "lexer.l"
+#line 539 "lexer.l"
/* skip whitespace */
YY_BREAK
case 74:
YY_RULE_SETUP
-#line 543 "lexer.l"
+#line 541 "lexer.l"
{
if (yytext[0] >= 32 && yytext[0] < 127)
@@ -1735,10 +1732,10 @@ YY_RULE_SETUP
YY_BREAK
case 75:
YY_RULE_SETUP
-#line 556 "lexer.l"
+#line 554 "lexer.l"
ECHO;
YY_BREAK
-#line 1742 "lexer.c"
+#line 1739 "lexer.c"
case YY_END_OF_BUFFER:
{
@@ -2871,7 +2868,7 @@ void yara_yyfree (void * ptr , yyscan_t yyscanner)
#define YYTABLES_NAME "yytables"
-#line 556 "lexer.l"
+#line 554 "lexer.l"
diff --git a/libyara/lexer.l b/libyara/lexer.l
index 2a5fee8..8a8fc84 100644
--- a/libyara/lexer.l
+++ b/libyara/lexer.l
@@ -161,8 +161,7 @@ include[ \t]+\" {
if (current_file_name != NULL)
{
- strncpy(buffer, current_file_name, sizeof(buffer)-1);
- buffer[sizeof(buffer)-1] = '\0';
+ strlcpy(buffer, current_file_name, sizeof(buffer));
}
else
{
@@ -180,8 +179,7 @@ include[ \t]+\" {
{
f = (b > s)? (b + 1): (s + 1);
- strncpy(f, yyextra->lex_buf, sizeof(buffer) - (f - buffer));
- buffer[sizeof(buffer)-1] = '\0';
+ strlcpy(f, yyextra->lex_buf, sizeof(buffer) - (f - buffer));
f = buffer;
diff --git a/libyara/modules/pe.c b/libyara/modules/pe.c
index d1ac58b..e10a95e 100644
--- a/libyara/modules/pe.c
+++ b/libyara/modules/pe.c
@@ -232,8 +232,7 @@ void parse_pe_header(
break;
}
- strncpy(section_name, (char*) section->Name, IMAGE_SIZEOF_SHORT_NAME);
- section_name[IMAGE_SIZEOF_SHORT_NAME] = '\0';
+ strlcpy(section_name, (char*) section->Name, IMAGE_SIZEOF_SHORT_NAME + 1);
set_string(
section_name,
diff --git a/libyara/re_lexer.c b/libyara/re_lexer.c
index 71c8795..2508391 100644
--- a/libyara/re_lexer.c
+++ b/libyara/re_lexer.c
@@ -47,7 +47,6 @@ typedef int16_t flex_int16_t;
typedef uint16_t flex_uint16_t;
typedef int32_t flex_int32_t;
typedef uint32_t flex_uint32_t;
-typedef uint64_t flex_uint64_t;
#else
typedef signed char flex_int8_t;
typedef short int flex_int16_t;
@@ -358,7 +357,7 @@ static void yy_fatal_error (yyconst char msg[] ,yyscan_t yyscanner );
*/
#define YY_DO_BEFORE_ACTION \
yyg->yytext_ptr = yy_bp; \
- yyleng = (yy_size_t) (yy_cp - yy_bp); \
+ yyleng = (size_t) (yy_cp - yy_bp); \
yyg->yy_hold_char = *yy_cp; \
*yy_cp = '\0'; \
yyg->yy_c_buf_p = yy_cp;
@@ -525,7 +524,7 @@ uint8_t read_escaped_char(yyscan_t yyscanner);
#define YY_NO_UNISTD_H 1
-#line 529 "re_lexer.c"
+#line 528 "re_lexer.c"
#define INITIAL 0
#define char_class 1
@@ -760,7 +759,7 @@ YY_DECL
#line 60 "re_lexer.l"
-#line 764 "re_lexer.c"
+#line 763 "re_lexer.c"
yylval = yylval_param;
@@ -1245,7 +1244,7 @@ YY_RULE_SETUP
#line 388 "re_lexer.l"
ECHO;
YY_BREAK
-#line 1249 "re_lexer.c"
+#line 1248 "re_lexer.c"
case YY_END_OF_BUFFER:
{
@@ -2482,7 +2481,7 @@ void yyerror(
{
lex_env->last_error_code = ERROR_INVALID_REGULAR_EXPRESSION;
- strncpy(
+ strlcpy(
lex_env->last_error_message,
error_message,
sizeof(lex_env->last_error_message));
@@ -2526,7 +2525,7 @@ int yr_parse_re_string(
yr_re_destroy(*re);
*re = NULL;
- strncpy(
+ strlcpy(
error->message,
lex_env.last_error_message,
sizeof(error->message));
diff --git a/libyara/re_lexer.l b/libyara/re_lexer.l
index 389431a..e62d7c5 100644
--- a/libyara/re_lexer.l
+++ b/libyara/re_lexer.l
@@ -487,7 +487,7 @@ void yyerror(
{
lex_env->last_error_code = ERROR_INVALID_REGULAR_EXPRESSION;
- strncpy(
+ strlcpy(
lex_env->last_error_message,
error_message,
sizeof(lex_env->last_error_message));
@@ -531,7 +531,7 @@ int yr_parse_re_string(
yr_re_destroy(*re);
*re = NULL;
- strncpy(
+ strlcpy(
error->message,
lex_env.last_error_message,
sizeof(error->message));
diff --git a/libyara/utils.c b/libyara/utils.c
index 84bb493..5334dfb 100644
--- a/libyara/utils.c
+++ b/libyara/utils.c
@@ -62,3 +62,81 @@ size_t xtoi(const char* hexstr)
return r;
}
+
+
+#if !defined(__FreeBSD__) && !defined(__OpenBSD__)
+
+/*
+
+strlcpy and strlcat are defined in FreeBSD and OpenBSD,
+the following implementations were taken from OpenBSD.
+
+*/
+
+
+size_t strlcpy(char *dst, const char *src, size_t size)
+{
+ register char *d = dst;
+ register const char *s = src;
+ register size_t n = size;
+
+ /* Copy as many bytes as will fit */
+
+ if (n != 0 && --n != 0)
+ {
+ do
+ {
+ if ((*d++ = *s++) == 0)
+ break;
+
+ } while (--n != 0);
+ }
+
+ /* Not enough room in dst, add NUL and traverse rest of src */
+
+ if (n == 0)
+ {
+ if (size != 0)
+ *d = '\0'; /* NUL-terminate dst */
+
+ while (*s++);
+ }
+
+ return(s - src - 1); /* count does not include NUL */
+}
+
+
+size_t strlcat(char *dst, const char *src, size_t size)
+{
+ register char *d = dst;
+ register const char *s = src;
+ register size_t n = size;
+ size_t dlen;
+
+ /* Find the end of dst and adjust bytes left but don't go past end */
+
+ while (n-- != 0 && *d != '\0')
+ d++;
+
+ dlen = d - dst;
+ n = size - dlen;
+
+ if (n == 0)
+ return(dlen + strlen(s));
+
+ while (*s != '\0')
+ {
+ if (n != 1)
+ {
+ *d++ = *s;
+ n--;
+ }
+ s++;
+ }
+
+ *d = '\0';
+
+ return(dlen + (s - src)); /* count does not include NUL */
+}
+
+#endif
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/forensics/yara.git
More information about the forensics-changes
mailing list