Skip to content
Browse files

Merge remote-tracking branch 'origin/mem_sys_strndup'

  • Loading branch information...
2 parents 930fa0f + 4d43e86 commit c282bfca03f1a45f9e0fb5c27b9e32452c50d187 @felipensp felipensp committed Jul 26, 2011
Showing with 57 additions and 24 deletions.
  1. +12 −12 compilers/imcc/imcc.l
  2. +12 −12 compilers/imcc/imclexer.c
  3. +8 −0 include/parrot/memory.h
  4. +25 −0 src/gc/alloc_memory.c
View
24 compilers/imcc/imcc.l
@@ -72,15 +72,15 @@ static int handle_identifier(ARGMOD(imc_info_t *imcc), YYSTYPE *valp, ARGIN(cons
#define DUP_AND_RET(valp, token) \
do { \
- if (valp) (valp)->s = mem_sys_strdup(yytext); \
+ if (valp) (valp)->s = mem_sys_strndup(yytext, yyleng); \
return (token); \
} while (0)
#define DUP_AND_RET_FREE(valp, token) \
do { \
if (valp) { \
mem_sys_free((valp)->s); \
- (valp)->s = mem_sys_strdup(yytext); \
+ (valp)->s = mem_sys_strndup(yytext, yyleng); \
return (token); \
} \
} while (0)
@@ -152,7 +152,7 @@ SP [ ]
<heredoc1>.*{EOL} {
SET_LINE_NUMBER;
- imcc->frames->heredoc_rest = mem_sys_strdup(yytext);
+ imcc->frames->heredoc_rest = mem_sys_strndup(yytext, yyleng);
BEGIN(heredoc2);
}
@@ -494,13 +494,13 @@ SP [ ]
YYCHOP();
if (valp)
- valp->s = mem_sys_strdup(yytext);
+ valp->s = mem_sys_strndup(yytext, yyleng);
return LABEL;
}
<emit,INITIAL>{DOT}{LETTER}{LETTERDIGIT}* {
- char * const macro_name = mem_sys_strdup(yytext + 1);
+ char * const macro_name = mem_sys_strndup(yytext + 1, yyleng - 1);
int failed = expand_macro(imcc, macro_name, yyscanner);
mem_sys_free(macro_name);
if (!failed) {
@@ -518,7 +518,7 @@ SP [ ]
<*>{OCT} DUP_AND_RET(valp, INTC);
<*>{STRINGCONSTANT} {
- valp->s = mem_sys_strdup(yytext);
+ valp->s = mem_sys_strndup(yytext, yyleng);
return STRINGC;
}
@@ -530,7 +530,7 @@ SP [ ]
off newline and quote. */
if (imcc->frames->heredoc_rest)
IMCC_fataly(imcc, EXCEPTION_SYNTAX_ERROR, "nested heredoc not supported");
- imcc->heredoc_end = mem_sys_strdup(yytext + 3);
+ imcc->heredoc_end = mem_sys_strndup(yytext + 3, yyleng - 3);
imcc->heredoc_end[strlen(imcc->heredoc_end) - 1] = 0;
if (!strlen(imcc->heredoc_end))
@@ -553,7 +553,7 @@ SP [ ]
<*>{UNICODE} {
/* charset:"..." */
- valp->s = mem_sys_strdup(yytext);
+ valp->s = mem_sys_strndup(yytext, yyleng);
/* this is actually not unicode but a string with a charset */
return USTRINGC;
@@ -605,7 +605,7 @@ SP [ ]
"'%s' is only a valid register name in PASM mode", yytext);
if (valp)
- valp->s = mem_sys_strdup(yytext);
+ valp->s = mem_sys_strndup(yytext, yyleng);
return REG;
}
@@ -874,7 +874,7 @@ read_params(YYSTYPE *valp, ARGMOD(imc_info_t *imcc), params_t *params,
{
YYSTYPE val;
size_t len = 0;
- char *current = mem_sys_strdup("");
+ char *current = mem_sys_strndup("", 0);
yyguts_t *yyg = (yyguts_t *)yyscanner;
int c = yylex_skip(&val, imcc, " \n", yyscanner);
@@ -896,7 +896,7 @@ read_params(YYSTYPE *valp, ARGMOD(imc_info_t *imcc), params_t *params,
MAX_PARAM, macro_name);
params->name[params->num_param++] = current;
- current = mem_sys_strdup("");
+ current = mem_sys_strndup("", 0);
len = 0;
if (val.s)
@@ -1138,7 +1138,7 @@ expand_macro(ARGMOD(imc_info_t *imcc), ARGIN(const char *name), void *yyscanner)
BEGIN(start_cond);
if (frame->expansion.num_param == 0 && m->params.num_param == 1) {
- frame->expansion.name[0] = mem_sys_strdup("");
+ frame->expansion.name[0] = mem_sys_strndup("", 0);
frame->expansion.num_param = 1;
}
View
24 compilers/imcc/imclexer.c
@@ -2200,15 +2200,15 @@ static int handle_identifier(ARGMOD(imc_info_t *imcc), YYSTYPE *valp, ARGIN(cons
#define DUP_AND_RET(valp, token) \
do { \
- if (valp) (valp)->s = mem_sys_strdup(yytext); \
+ if (valp) (valp)->s = mem_sys_strndup(yytext, yyleng); \
return (token); \
} while (0)
#define DUP_AND_RET_FREE(valp, token) \
do { \
if (valp) { \
mem_sys_free((valp)->s); \
- (valp)->s = mem_sys_strdup(yytext); \
+ (valp)->s = mem_sys_strndup(yytext, yyleng); \
return (token); \
} \
} while (0)
@@ -2593,7 +2593,7 @@ YY_RULE_SETUP
#line 153 "compilers/imcc/imcc.l"
{
SET_LINE_NUMBER;
- imcc->frames->heredoc_rest = mem_sys_strdup(yytext);
+ imcc->frames->heredoc_rest = mem_sys_strndup(yytext, yyleng);
BEGIN(heredoc2);
}
YY_BREAK
@@ -3359,7 +3359,7 @@ YY_RULE_SETUP
YYCHOP();
if (valp)
- valp->s = mem_sys_strdup(yytext);
+ valp->s = mem_sys_strndup(yytext, yyleng);
return LABEL;
}
@@ -3368,7 +3368,7 @@ case 115:
YY_RULE_SETUP
#line 502 "compilers/imcc/imcc.l"
{
- char * const macro_name = mem_sys_strdup(yytext + 1);
+ char * const macro_name = mem_sys_strndup(yytext + 1, yyleng - 1);
int failed = expand_macro(imcc, macro_name, yyscanner);
mem_sys_free(macro_name);
if (!failed) {
@@ -3406,7 +3406,7 @@ case 121:
YY_RULE_SETUP
#line 520 "compilers/imcc/imcc.l"
{
- valp->s = mem_sys_strdup(yytext);
+ valp->s = mem_sys_strndup(yytext, yyleng);
return STRINGC;
}
@@ -3421,7 +3421,7 @@ YY_RULE_SETUP
off newline and quote. */
if (imcc->frames->heredoc_rest)
IMCC_fataly(imcc, EXCEPTION_SYNTAX_ERROR, "nested heredoc not supported");
- imcc->heredoc_end = mem_sys_strdup(yytext + 3);
+ imcc->heredoc_end = mem_sys_strndup(yytext + 3, yyleng - 3);
imcc->heredoc_end[strlen(imcc->heredoc_end) - 1] = 0;
if (!strlen(imcc->heredoc_end))
@@ -3447,7 +3447,7 @@ YY_RULE_SETUP
#line 554 "compilers/imcc/imcc.l"
{
/* charset:"..." */
- valp->s = mem_sys_strdup(yytext);
+ valp->s = mem_sys_strndup(yytext, yyleng);
/* this is actually not unicode but a string with a charset */
return USTRINGC;
@@ -3517,7 +3517,7 @@ YY_RULE_SETUP
"'%s' is only a valid register name in PASM mode", yytext);
if (valp)
- valp->s = mem_sys_strdup(yytext);
+ valp->s = mem_sys_strndup(yytext, yyleng);
return REG;
}
@@ -5059,7 +5059,7 @@ read_params(YYSTYPE *valp, ARGMOD(imc_info_t *imcc), params_t *params,
{
YYSTYPE val;
size_t len = 0;
- char *current = mem_sys_strdup("");
+ char *current = mem_sys_strndup("", 0);
yyguts_t *yyg = (yyguts_t *)yyscanner;
int c = yylex_skip(&val, imcc, " \n", yyscanner);
@@ -5081,7 +5081,7 @@ read_params(YYSTYPE *valp, ARGMOD(imc_info_t *imcc), params_t *params,
MAX_PARAM, macro_name);
params->name[params->num_param++] = current;
- current = mem_sys_strdup("");
+ current = mem_sys_strndup("", 0);
len = 0;
if (val.s)
@@ -5323,7 +5323,7 @@ expand_macro(ARGMOD(imc_info_t *imcc), ARGIN(const char *name), void *yyscanner)
BEGIN(start_cond);
if (frame->expansion.num_param == 0 && m->params.num_param == 1) {
- frame->expansion.name[0] = mem_sys_strdup("");
+ frame->expansion.name[0] = mem_sys_strndup("", 0);
frame->expansion.num_param = 1;
}
View
8 include/parrot/memory.h
@@ -103,6 +103,12 @@ PARROT_MALLOC
PARROT_CANNOT_RETURN_NULL
char * mem_sys_strdup(ARGIN(const char *src))
__attribute__nonnull__(1);
+
+PARROT_EXPORT
+PARROT_MALLOC
+PARROT_CANNOT_RETURN_NULL
+char * mem_sys_strndup(ARGIN(const char *src), size_t size)
+ __attribute__nonnull__(1);
#define ASSERT_ARGS_mem_sys_allocate __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_mem_sys_allocate_zeroed __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
@@ -111,6 +117,8 @@ char * mem_sys_strdup(ARGIN(const char *src))
#define ASSERT_ARGS_mem_sys_realloc_zeroed __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_mem_sys_strdup __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(src))
+#define ASSERT_ARGS_mem_sys_strndup __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
+ PARROT_ASSERT_ARG(src))
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
/* HEADERIZER END: src/gc/alloc_memory.c */
View
25 src/gc/alloc_memory.c
@@ -206,6 +206,31 @@ mem_sys_free(ARGFREE(void *from))
/*
+=item C<char * mem_sys_strndup(const char *src, size_t size)>
+
+Copy a C string with supplied size to a new block of memory allocated with
+mem_sys_allocate, that can be later deallocated with mem_sys_free.
+
+=cut
+
+*/
+
+PARROT_EXPORT
+PARROT_MALLOC
+PARROT_CANNOT_RETURN_NULL
+char *
+mem_sys_strndup(ARGIN(const char *src), size_t size)
+{
+ ASSERT_ARGS(mem_sys_strndup)
+
+ char * const result = (char *)mem_sys_allocate(size + 1);
+ memcpy(result, src, size);
+ result[size] = '\0';
+ return result;
+}
+
+/*
+
=item C<char * mem_sys_strdup(const char *src)>
Copy a C string to a new block of memory allocated with mem_sys_allocate,

0 comments on commit c282bfc

Please sign in to comment.
Something went wrong with that request. Please try again.