Skip to content

Commit

Permalink
Fixed uninitialised memory read in file open ops.
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.parrot.org/parrot/trunk@1336 d31e2699-5ff4-0310-a27c-f18f2fbe73fe
  • Loading branch information
tomhughes committed Apr 14, 2002
1 parent 1b6c5da commit 45ab06a
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 12 deletions.
12 changes: 6 additions & 6 deletions core.ops
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ descriptor into $1.
=cut

inline op open(out INT, in STR) {
$1 = (INTVAL)fopen(($2)->bufstart, "r+");
$1 = (INTVAL)fopen(string_to_cstring(interpreter, ($2)), "r+");
if (!$1) {
perror("Can't open");
exit(1);
Expand All @@ -145,7 +145,7 @@ inline op open(out INT, in STR) {
}

inline op open(out INT, in STR, in STR) {
$1 = (INTVAL)fopen(($2)->bufstart, ($3)->bufstart);
$1 = (INTVAL)fopen(string_to_cstring(interpreter, ($2)), string_to_cstring(interpreter, ($3)));
goto NEXT();
}

Expand Down Expand Up @@ -246,7 +246,7 @@ inline op print(in NUM) {
op print(in STR) {
STRING *s = $1;
if (s && string_length(s)) {
printf("%.*s", (int)string_length(s), (char *) s->bufstart);
printf("%s", string_to_cstring(interpreter, (s)));
}
goto NEXT();
}
Expand All @@ -255,7 +255,7 @@ op print(in PMC) {
PMC *p = $1;
STRING *s = (p->vtable->get_string(interpreter, p));
if (s) {
printf("%.*s",(int)string_length(s),(char *) s->bufstart);
printf("%s", string_to_cstring(interpreter, (s)));
}
goto NEXT();
}
Expand Down Expand Up @@ -304,7 +304,7 @@ op print(in INT, in STR) {
default: file = (FILE *)$1;
}
if (s && string_length(s)) {
fprintf(file, "%.*s",(int)string_length(s),(char *) s->bufstart);
fprintf(file, "%s", string_to_cstring(interpreter, (s)));
}
goto NEXT();
}
Expand All @@ -323,7 +323,7 @@ op print(in INT, in PMC) {
default: file = (FILE *)$1;
}
if (s) {
fprintf(file, "%.*s",(int)string_length(s),(char *) s->bufstart);
fprintf(file, "%s", string_to_cstring(interpreter, (s)));
}
goto NEXT();
}
Expand Down
4 changes: 4 additions & 0 deletions include/parrot/string_funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ STRING *Parrot_string_replace(Parrot, STRING *, INTVAL, INTVAL,
const STRING *, STRING **);
INTVAL Parrot_string_compare(Parrot, const STRING *, const STRING *);
Parrot_Bool Parrot_string_bool(const STRING *);
const char *Parrot_string_cstring(const STRING *);

/* Declarations of other functions */
UINTVAL Parrot_string_length(const STRING *);
Expand All @@ -45,6 +46,7 @@ STRING *Parrot_string_transcode(struct Parrot_Interp *, const STRING *src,
STRING **dest_ptr);
void Parrot_string_init(void);
INTVAL Parrot_string_index(const STRING *, UINTVAL idx);
const char *Parrot_string_to_cstring(struct Parrot_Interp *, STRING *);

#ifdef PARROT_IN_CORE

Expand All @@ -56,6 +58,7 @@ INTVAL Parrot_string_index(const STRING *, UINTVAL idx);
#define string_replace Parrot_string_replace
#define string_compare Parrot_string_compare
#define string_bool Parrot_string_bool
#define string_cstring Parrot_string_cstring

#define string_length Parrot_string_length
#define string_ord Parrot_string_ord
Expand All @@ -69,6 +72,7 @@ INTVAL Parrot_string_index(const STRING *, UINTVAL idx);
#define string_transcode Parrot_string_transcode
#define string_init Parrot_string_init
#define string_index Parrot_string_index
#define string_to_cstring Parrot_string_to_cstring

#endif

Expand Down
12 changes: 6 additions & 6 deletions ops/core.ops
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ descriptor into $1.
=cut

inline op open(out INT, in STR) {
$1 = (INTVAL)fopen(($2)->bufstart, "r+");
$1 = (INTVAL)fopen(string_to_cstring(interpreter, ($2)), "r+");
if (!$1) {
perror("Can't open");
exit(1);
Expand All @@ -145,7 +145,7 @@ inline op open(out INT, in STR) {
}

inline op open(out INT, in STR, in STR) {
$1 = (INTVAL)fopen(($2)->bufstart, ($3)->bufstart);
$1 = (INTVAL)fopen(string_to_cstring(interpreter, ($2)), string_to_cstring(interpreter, ($3)));
goto NEXT();
}

Expand Down Expand Up @@ -246,7 +246,7 @@ inline op print(in NUM) {
op print(in STR) {
STRING *s = $1;
if (s && string_length(s)) {
printf("%.*s", (int)string_length(s), (char *) s->bufstart);
printf("%s", string_to_cstring(interpreter, (s)));
}
goto NEXT();
}
Expand All @@ -255,7 +255,7 @@ op print(in PMC) {
PMC *p = $1;
STRING *s = (p->vtable->get_string(interpreter, p));
if (s) {
printf("%.*s",(int)string_length(s),(char *) s->bufstart);
printf("%s", string_to_cstring(interpreter, (s)));
}
goto NEXT();
}
Expand Down Expand Up @@ -304,7 +304,7 @@ op print(in INT, in STR) {
default: file = (FILE *)$1;
}
if (s && string_length(s)) {
fprintf(file, "%.*s",(int)string_length(s),(char *) s->bufstart);
fprintf(file, "%s", string_to_cstring(interpreter, (s)));
}
goto NEXT();
}
Expand All @@ -323,7 +323,7 @@ op print(in INT, in PMC) {
default: file = (FILE *)$1;
}
if (s) {
fprintf(file, "%.*s",(int)string_length(s),(char *) s->bufstart);
fprintf(file, "%s", string_to_cstring(interpreter, (s)));
}
goto NEXT();
}
Expand Down
15 changes: 15 additions & 0 deletions src/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,21 @@ string_from_int(struct Parrot_Interp * interpreter, INTVAL i) {
NULL, 0, NULL);
}

const char *
string_to_cstring(struct Parrot_Interp * interpreter, STRING * s)
{
char *cstring;

if (s->buflen == s->bufused)
string_grow(interpreter, s, 1);

cstring = s->bufstart;

cstring[s->bufused] = 0;

return cstring;
}


/*
* Local variables:
Expand Down
15 changes: 15 additions & 0 deletions string.c
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,21 @@ string_from_int(struct Parrot_Interp * interpreter, INTVAL i) {
NULL, 0, NULL);
}

const char *
string_to_cstring(struct Parrot_Interp * interpreter, STRING * s)
{
char *cstring;

if (s->buflen == s->bufused)
string_grow(interpreter, s, 1);

cstring = s->bufstart;

cstring[s->bufused] = 0;

return cstring;
}


/*
* Local variables:
Expand Down

0 comments on commit 45ab06a

Please sign in to comment.