Commit 6c9bb239 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge pull request #147 from undingen/mem_corruption

Fix a memory corruption in posix.urandom
parents 6eaa7283 fa4bf00c
...@@ -345,6 +345,7 @@ void registerMainThread() { ...@@ -345,6 +345,7 @@ void registerMainThread() {
current_threads[gettid()] = new ThreadStateInternal(find_stack(), pthread_self()); current_threads[gettid()] = new ThreadStateInternal(find_stack(), pthread_self());
struct sigaction act; struct sigaction act;
memset(&act, 0, sizeof(act));
act.sa_flags = SA_SIGINFO; act.sa_flags = SA_SIGINFO;
act.sa_sigaction = _thread_context_dump; act.sa_sigaction = _thread_context_dump;
struct sigaction oldact; struct sigaction oldact;
......
...@@ -90,6 +90,8 @@ extern "C" Box* abs_(Box* x) { ...@@ -90,6 +90,8 @@ extern "C" Box* abs_(Box* x) {
} else if (x->cls == float_cls) { } else if (x->cls == float_cls) {
double d = static_cast<BoxedFloat*>(x)->d; double d = static_cast<BoxedFloat*>(x)->d;
return boxFloat(d >= 0 ? d : -d); return boxFloat(d >= 0 ? d : -d);
} else if (x->cls == long_cls) {
return longAbs(static_cast<BoxedLong*>(x));
} else { } else {
RELEASE_ASSERT(0, "%s", getTypeName(x)->c_str()); RELEASE_ASSERT(0, "%s", getTypeName(x)->c_str());
} }
......
...@@ -39,7 +39,7 @@ Box* urandom(Box* _n) { ...@@ -39,7 +39,7 @@ Box* urandom(Box* _n) {
int fd = ::open("/dev/urandom", O_RDONLY); int fd = ::open("/dev/urandom", O_RDONLY);
RELEASE_ASSERT(fd > 0, ""); RELEASE_ASSERT(fd > 0, "");
BoxedString* r = static_cast<BoxedString*>(PyString_FromStringAndSize(NULL, sizeof(n))); BoxedString* r = static_cast<BoxedString*>(PyString_FromStringAndSize(NULL, n));
RELEASE_ASSERT(r, ""); RELEASE_ASSERT(r, "");
char* buf = PyString_AsString(r); char* buf = PyString_AsString(r);
...@@ -49,6 +49,7 @@ Box* urandom(Box* _n) { ...@@ -49,6 +49,7 @@ Box* urandom(Box* _n) {
assert(this_read > 0); assert(this_read > 0);
total_read += this_read; total_read += this_read;
} }
::close(fd);
return r; return r;
} }
......
...@@ -224,6 +224,14 @@ Box* longNeg(BoxedLong* v1) { ...@@ -224,6 +224,14 @@ Box* longNeg(BoxedLong* v1) {
return r; return r;
} }
Box* longAbs(BoxedLong* v1) {
assert(isSubclass(v1->cls, long_cls));
BoxedLong* r = new BoxedLong(long_cls);
mpz_init(r->n);
mpz_abs(r->n, v1->n);
return r;
}
Box* longAdd(BoxedLong* v1, Box* _v2) { Box* longAdd(BoxedLong* v1, Box* _v2) {
if (!isSubclass(v1->cls, long_cls)) if (!isSubclass(v1->cls, long_cls))
raiseExcHelper(TypeError, "descriptor '__add__' requires a 'long' object but received a '%s'", raiseExcHelper(TypeError, "descriptor '__add__' requires a 'long' object but received a '%s'",
......
...@@ -37,6 +37,7 @@ extern "C" Box* createLong(const std::string* s); ...@@ -37,6 +37,7 @@ extern "C" Box* createLong(const std::string* s);
extern "C" BoxedLong* boxLong(int64_t n); extern "C" BoxedLong* boxLong(int64_t n);
Box* longNeg(BoxedLong* lhs); Box* longNeg(BoxedLong* lhs);
Box* longAbs(BoxedLong* v1);
Box* longAdd(BoxedLong* lhs, Box* rhs); Box* longAdd(BoxedLong* lhs, Box* rhs);
Box* longSub(BoxedLong* lhs, Box* rhs); Box* longSub(BoxedLong* lhs, Box* rhs);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment