Skip to content

Instantly share code, notes, and snippets.

#include <string.h>
#include <stdio.h>
int main(void)
{
char *str = "12345";
char *buf = malloc(5);
strcpy(buf, str);
#include <string.h>
#include <stdio.h>
int main(void)
{
char *str = "12345";
char buf[5];
strcpy(buf, str);
diff --git a/configure.in b/configure.in
index 599775b..84f10af 100644
--- a/configure.in
+++ b/configure.in
@@ -980,7 +980,7 @@ AC_CHECK_HEADERS(limits.h sys/file.h sys/ioctl.h sys/sysca
syscall.h pwd.h grp.h a.out.h utime.h direct.h sys/resource.h
sys/mkdev.h sys/utime.h float.h ieeefp.h \
ucontext.h intrinsics.h langinfo.h locale.h sys/sendfile.h ti
- net/socket.h sys/socket.h process.h sys/prctl.h)
+ net/socket.h sys/socket.h process.h sys/prctl.h atomic.h)
glibcでrealloc(ptr, 0)とわたしたときに、mtraceのログがおかしいというバグがあってその議論が面白かったのでご紹介。
<a href="http://sourceware.org/bugzilla/show_bug.cgi?id=14981" target="_blank" title="http://sourceware.org/bugzilla/show_bug.cgi?id=14981">http://sourceware.org/bugzilla/show_bug.cgi?id=14981</a>
reallocという関数がある。まあ、みなさんよくご存知のように realloc(ptr, size)とわたしたときにptrをsizeの大きさに拡張(縮小)してくれる関数である。
malloc(0)がNULLを返しても非NULLを返してもいいように、realloc(ptr, 0)も挙動に実装の自由度がある
1.realloc(ptr, 1) と同等(mallocが非NULLを返すケースではこれが普通)
2.ptrは解放され、NULLが返る(mallocがNULLを返すケースでは常識的な挙動)
3.ptrは解放されず、NULLが返る(mallocがNULLを返すケースでは同じくらい常識的な挙動、malloc(0)をエラーとみなす哲学だと関数の最初に引数チェックをするのは理解できる)
この記事は Ruby の Advent Calendar に参加しようとして用意しましたが、間に合わなかったものです。
Ruby 2.0でdtrace対応が入りました。この機能はLinuxのsystemtapからもアクセス出来ます。でも、どこにもドキュメントがないのでいっちょ使い方を解説しようという、そういう趣旨の記事です。
まず、基本の使い方ですが、以下の様に
process(プロセス名).provider("ruby").mark(Rubyで定義されてるprobe名) で引っ掛けるイベント名を
記述して、tapscriptを記述します
ruby.stp
<blockquote><pre>probe process("./ruby").provider("ruby").mark("find__require__entry")
diff --git a/thread_pthread.c b/thread_pthread.c
index d60cb41..4d055da 100644
--- a/thread_pthread.c
+++ b/thread_pthread.c
@@ -959,11 +959,13 @@ native_sleep(rb_thread_t *th, struct timeval *timeout_tv)
GVL_UNLOCK_BEGIN();
{
+ rb_thread_t *th = ruby_thread_from_native();
+
50.times{|e|
(1..2).map{
Thread.new{
}
}.each{|e|
e.join()
}
}
mutex = Mutex.new
Thread.async_interrupt_timing(Object => :on_blocking) {
mutex.synchronize {
begin
sleep 1
condvar.wait mutex
ensure
リソース解放したい
end
diff --git a/thread.c b/thread.c
index 43b1ad1..3e58c9c 100644
--- a/thread.c
+++ b/thread.c
@@ -1545,12 +1545,27 @@ rb_threadptr_async_errinfo_active_p(rb_thread_t *th)
return 1;
}
+static int
+async_interrupt_timing_arg_check_i(VALUE key, VALUE val)
diff --git a/ChangeLog b/ChangeLog
index f2f2cf1..378c63b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+Sat Dec 1 01:19:34 2012 KOSAKI Motohiro <kosaki.motohiro@gmail.com>
+
+ * lib/thread.rb (ConditionVariable#broadcast): protect from
+ async interrupt by using Thread.async_interrupt_timing.
+ * lib/thread.rb (ConditionVariable#signal): ditto.