|
| 1 | +From 17aa5c633d13f3ef6dcbec7f08a01b20ed4fbc12 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Sutou Kouhei < [email protected]> |
| 3 | +Date: Thu, 15 Sep 2022 07:08:20 +0900 |
| 4 | +Subject: [PATCH] merge revision(s) a4ad6bd9aac564e93219284c912b26a72f9e82fc: |
| 5 | + |
| 6 | + [ruby/fiddle] closure: free resources when an exception is raised in |
| 7 | + Closure.new |
| 8 | + |
| 9 | + GitHub: GH-102 |
| 10 | + |
| 11 | + https://github.com/ruby/fiddle/commit/81a8a56239 |
| 12 | + --- |
| 13 | + ext/fiddle/closure.c | 56 ++++++++++++++++++++++++++++++++++++++++------------ |
| 14 | + 1 file changed, 43 insertions(+), 13 deletions(-) |
| 15 | +--- |
| 16 | + ext/fiddle/closure.c | 56 ++++++++++++++++++++++++++++++++++---------- |
| 17 | + version.h | 6 ++--- |
| 18 | + 2 files changed, 46 insertions(+), 16 deletions(-) |
| 19 | + |
| 20 | +diff --git a/ext/fiddle/closure.c b/ext/fiddle/closure.c |
| 21 | +index 27f448a24f0108..c08ec5940da84c 100644 |
| 22 | +--- a/ext/fiddle/closure.c |
| 23 | ++++ b/ext/fiddle/closure.c |
| 24 | +@@ -224,9 +224,16 @@ allocate(VALUE klass) |
| 25 | + return i; |
| 26 | + } |
| 27 | + |
| 28 | ++typedef struct { |
| 29 | ++ VALUE self; |
| 30 | ++ int argc; |
| 31 | ++ VALUE *argv; |
| 32 | ++} initialize_data; |
| 33 | ++ |
| 34 | + static VALUE |
| 35 | +-initialize(int rbargc, VALUE argv[], VALUE self) |
| 36 | ++initialize_body(VALUE user_data) |
| 37 | + { |
| 38 | ++ initialize_data *data = (initialize_data *)user_data; |
| 39 | + VALUE ret; |
| 40 | + VALUE args; |
| 41 | + VALUE normalized_args; |
| 42 | +@@ -237,14 +244,14 @@ initialize(int rbargc, VALUE argv[], VALUE self) |
| 43 | + ffi_status result; |
| 44 | + int i, argc; |
| 45 | + |
| 46 | +- if (2 == rb_scan_args(rbargc, argv, "21", &ret, &args, &abi)) |
| 47 | +- abi = INT2NUM(FFI_DEFAULT_ABI); |
| 48 | ++ if (2 == rb_scan_args(data->argc, data->argv, "21", &ret, &args, &abi)) |
| 49 | ++ abi = INT2NUM(FFI_DEFAULT_ABI); |
| 50 | + |
| 51 | + Check_Type(args, T_ARRAY); |
| 52 | + |
| 53 | + argc = RARRAY_LENINT(args); |
| 54 | + |
| 55 | +- TypedData_Get_Struct(self, fiddle_closure, &closure_data_type, cl); |
| 56 | ++ TypedData_Get_Struct(data->self, fiddle_closure, &closure_data_type, cl); |
| 57 | + |
| 58 | + cl->argv = (ffi_type **)xcalloc(argc + 1, sizeof(ffi_type *)); |
| 59 | + |
| 60 | +@@ -257,8 +264,8 @@ initialize(int rbargc, VALUE argv[], VALUE self) |
| 61 | + cl->argv[argc] = NULL; |
| 62 | + |
| 63 | + ret = rb_fiddle_type_ensure(ret); |
| 64 | +- rb_iv_set(self, "@ctype", ret); |
| 65 | +- rb_iv_set(self, "@args", normalized_args); |
| 66 | ++ rb_iv_set(data->self, "@ctype", ret); |
| 67 | ++ rb_iv_set(data->self, "@args", normalized_args); |
| 68 | + |
| 69 | + cif = &cl->cif; |
| 70 | + pcl = cl->pcl; |
| 71 | +@@ -269,25 +276,48 @@ initialize(int rbargc, VALUE argv[], VALUE self) |
| 72 | + rb_fiddle_int_to_ffi_type(NUM2INT(ret)), |
| 73 | + cl->argv); |
| 74 | + |
| 75 | +- if (FFI_OK != result) |
| 76 | +- rb_raise(rb_eRuntimeError, "error prepping CIF %d", result); |
| 77 | ++ if (FFI_OK != result) { |
| 78 | ++ rb_raise(rb_eRuntimeError, "error prepping CIF %d", result); |
| 79 | ++ } |
| 80 | + |
| 81 | + #if USE_FFI_CLOSURE_ALLOC |
| 82 | + result = ffi_prep_closure_loc(pcl, cif, callback, |
| 83 | +- (void *)self, cl->code); |
| 84 | ++ (void *)(data->self), cl->code); |
| 85 | + #else |
| 86 | + result = ffi_prep_closure(pcl, cif, callback, (void *)(data->self)); |
| 87 | + cl->code = (void *)pcl; |
| 88 | + i = mprotect(pcl, sizeof(*pcl), PROT_READ | PROT_EXEC); |
| 89 | + if (i) { |
| 90 | +- rb_sys_fail("mprotect"); |
| 91 | ++ rb_sys_fail("mprotect"); |
| 92 | + } |
| 93 | + #endif |
| 94 | + |
| 95 | +- if (FFI_OK != result) |
| 96 | +- rb_raise(rb_eRuntimeError, "error prepping closure %d", result); |
| 97 | ++ if (FFI_OK != result) { |
| 98 | ++ rb_raise(rb_eRuntimeError, "error prepping closure %d", result); |
| 99 | ++ } |
| 100 | ++ |
| 101 | ++ return data->self; |
| 102 | ++} |
| 103 | + |
| 104 | +- return self; |
| 105 | ++static VALUE |
| 106 | ++initialize_rescue(VALUE user_data, VALUE exception) |
| 107 | ++{ |
| 108 | ++ initialize_data *data = (initialize_data *)user_data; |
| 109 | ++ dealloc(RTYPEDDATA_DATA(data->self)); |
| 110 | ++ RTYPEDDATA_DATA(data->self) = NULL; |
| 111 | ++ rb_exc_raise(exception); |
| 112 | ++ return data->self; |
| 113 | ++} |
| 114 | ++ |
| 115 | ++static VALUE |
| 116 | ++initialize(int argc, VALUE *argv, VALUE self) |
| 117 | ++{ |
| 118 | ++ initialize_data data; |
| 119 | ++ data.self = self; |
| 120 | ++ data.argc = argc; |
| 121 | ++ data.argv = argv; |
| 122 | ++ return rb_rescue(initialize_body, (VALUE)&data, |
| 123 | ++ initialize_rescue, (VALUE)&data); |
| 124 | + } |
| 125 | + |
| 126 | + static VALUE |
| 127 | +diff --git a/version.h b/version.h |
| 128 | +index 7c8bc046b33c54..99b715563a17ba 100644 |
| 129 | +--- a/version.h |
| 130 | ++++ b/version.h |
| 131 | +@@ -11,11 +11,11 @@ |
| 132 | + # define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR |
| 133 | + #define RUBY_VERSION_TEENY 5 |
| 134 | + #define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR |
| 135 | +-#define RUBY_PATCHLEVEL 252 |
| 136 | ++#define RUBY_PATCHLEVEL 253 |
| 137 | + |
| 138 | + #define RUBY_RELEASE_YEAR 2024 |
| 139 | +-#define RUBY_RELEASE_MONTH 4 |
| 140 | +-#define RUBY_RELEASE_DAY 23 |
| 141 | ++#define RUBY_RELEASE_MONTH 5 |
| 142 | ++#define RUBY_RELEASE_DAY 2 |
| 143 | + |
| 144 | + #include "ruby/version.h" |
| 145 | + |
0 commit comments