ext/oj/fast.c in oj-1.4.0 vs ext/oj/fast.c in oj-1.4.1

- old
+ new

@@ -26,11 +26,13 @@ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include <sys/resource.h> +#if !IS_WINDOWS +#include <sys/resource.h> // for getrlimit() on linux +#endif #include <stdlib.h> #include <stdio.h> #include <string.h> #include <math.h> #include <errno.h> @@ -477,11 +479,15 @@ static Leaf read_next(ParseInfo pi) { Leaf leaf = 0; +#if IS_WINDOWS + if ((uint64_t)(uint32_t)&leaf < pi->stack_min) { +#else if ((uint64_t)&leaf < pi->stack_min) { +#endif rb_raise(rb_eSysStackError, "JSON is too deeply nested"); } next_non_white(pi); // skip white space switch (*pi->s) { case '{': @@ -830,25 +836,32 @@ parse_json(VALUE clas, char *json, int given, int allocated) { struct _ParseInfo pi; VALUE result = Qnil; Doc doc; int ex = 0; - struct rlimit lim; if (given) { doc = ALLOCA_N(struct _Doc, 1); } else { doc = ALLOC_N(struct _Doc, 1); } pi.str = json; pi.s = pi.str; doc_init(doc); pi.doc = doc; - if (0 == getrlimit(RLIMIT_STACK, &lim)) { - pi.stack_min = (uint64_t)&lim - (lim.rlim_cur / 4 * 3); // let 3/4ths of the stack be used only - } else { - pi.stack_min = 0; // indicates not to check stack limit +#if IS_WINDOWS + pi.stack_min = (uint64_t)(uint32_t)&pi - (512 * 1024); // assume a 1M stack and give half to ruby +#else + { + struct rlimit lim; + + if (0 == getrlimit(RLIMIT_STACK, &lim)) { + pi.stack_min = (uint64_t)&pi - (lim.rlim_cur / 4 * 3); // let 3/4ths of the stack be used only + } else { + pi.stack_min = 0; // indicates not to check stack limit + } } +#endif // last arg is free func void* func(void*) doc->self = rb_data_object_alloc(clas, doc, 0, free_doc_cb); rb_gc_register_address(&doc->self); doc->json = json; DATA_PTR(doc->self) = doc;