summaryrefslogtreecommitdiffstats
Commit message (Collapse)AuthorAgeFilesLines
* New FFI types str-s, bstr-s and wstr-s.Kaz Kylheku2022-03-214-54/+143
| | | | | | | | | | | | | | | | | These types actually make it possible to receive a string by pointer from a C function, without trying to free it. It is now possible to write a FFI wrapper for strtol or wcstol, which is done in the new test case. * ffi.c (str_s_s, bstr_s_s, wstr_s_s): New symbol variables. (ffi_init_types): Register the types str-s, bstr-s and wstr-s. (ffi_init): Intern the new symbols. * tests/017/str-s.tl: New file. * txr.1: Documented. * stdlib/doc-syms.tl: Updated.
* ffi: minor code reordering in initialization.Kaz Kylheku2022-03-201-4/+6
| | | | | * ffi.c (ffi_init_types): Move initialization of str-d type from just before wstr to after, for consistency.
* configure: bugfix in shell escaping codeKaz Kylheku2022-03-181-1/+1
| | | | | | | | | | | * configure: in the case when the argument contains single quotes, we try to identify the subcase that cannot use double quotes instead. That subcase is not correctly identified: it occurs when there are double quotes, dollar signs **or backslashes**. For instance the data '\ cannot be put into double quotes as "'\" on account of the backslash, which is an active character in double quotes. We also simplify the pattern by using a character class.
* configure: missing tab in shell escaping code.Kaz Kylheku2022-03-181-1/+1
| | | | | | * configure: a certain ' ' (four spaces between single quotes is actually supposed to be a tab; it must have been expanded by the editor.
* ffi: after call, process args in reverse order.Kaz Kylheku2022-03-171-11/+10
| | | | | | | | | | | | | | | | | | | | | | | | | In FFI foreign function and closure calls, it behooves us to iterate over the arguments in the opposite order after the call is done. By doing that, any freeing of memory that will be done will be in the opposite order. And that is necessary if some left argument allocates a pointer that is shared with a right argument. An example of this occurs in the strtol function: long int strtol(const char *nptr, char **endptr, int base); Here, the function sets *endptr to a value derived by displacing ptr. If we call this function via FFI, then FFI will allocate memory for nptr. When the function returns, if we destroy that memory before processing *endptr then *endptr contains an invalid pointer. * ffi.c (ffi_call_wrap): In the in pass, iterate arguments in reverse. (ffi_closure_dispatch, ffi_closure_dispatch_safe): Stack-allocate an array for the argument types, so we don't have to pop the list twice, and to allow access in reverse order. In the out pass, iterate arguments in reverse.
* doc: sort-group: unnecessary copyKaz Kylheku2022-03-161-1/+1
| | | | | * txr.1: Remove copy operation partition-by expression; this was needed when sort was destructive.
* ffi: support intmax-t and uintmax-t types.Kaz Kylheku2022-03-154-9/+62
| | | | | | | | | | | | | * configure: detect intmax_t and place HAVE_INTMAX_T into config.h. * ffi.c (ffi_init_extra_types): register intmax-t and uintmax-t types. If HAVE_INTMAX_T is missing, then make them aliases for longlong and ulonglong. * txr.1: Documented. * stdlib/doc-syms.tl: Updated.
* New GNU-themed quip.Kaz Kylheku2022-03-081-0/+1
| | | | | | * stdlib/quips.tl (%quips%): Samsung spelled backwards is gnusmaS. In December, don't wish Merry GNUsmas in a GNU mailing list.
* New function: group-map.Kaz Kylheku2022-03-025-9/+51
| | | | | | | | | | | | * hash.c (group_map): New function. (hash_init): group-map intrinsic registered. * hash.h (group_map): Declared. * tests/010/hash.tl: New test case. * txr.1: Documented together with group-by. Extra paren removed from group-by example.
* hash: add tests for group-by, group-reduce.Kaz Kylheku2022-03-021-0/+16
| | | | * tests/010/hash.tl: New tests.
* hash: group-reduce calls hash-update.Kaz Kylheku2022-03-021-9/+3
| | | | | | | * hash.c (group_reduce): Replace loop with call to hash_update which is exactly the same logic, and even more efficient because it avoids calling us_rplacd. (hash_update): Fix incorrect self name.
* Version 274.txr-274Kaz Kylheku2022-02-247-859/+1003
| | | | | | | | | | | | | | * RELNOTES: Updated. * configure (txr_ver): Bumped version. * stdlib/ver.tl (lib-version): Bumped. * txr.1: Bumped version and date. * txr.vim, tl.vim: Regenerated. * protsym.c: Likewise.
* opip: pass through forms in the op family.Kaz Kylheku2022-02-243-5/+28
| | | | | | | | | | | | | | * stdlib/op.tl (sys:opip-expand): Add op, do, lop, ldo, ap, ip, ado, ido, ret and aret to the operators whose forms are passed through untransformed. This is important because it lets us override the implicit (op ...) and (do ...) chosen by the expander. When a pipeline element produces a list, for instance, we want to be able to use (ap ...) in the next element to spread the list into arguments. * tests/012/op.tl: Add bellied numbers test case. * txr.1: Documented.
* doc: revisit partition-ifKaz Kylheku2022-02-231-7/+26
| | | | | * txr.1: Add introductory paragraph, and fine-tune the rest of the documentation.
* doc: make-like can take an iterators.Kaz Kylheku2022-02-231-0/+10
| | | | | | * txr.1: A December 2021 change that went into TXR 273 allows iterators from iter-begin to be the object argument to the make-like function. This is now documented.
* New list-builder method: oust.Kaz Kylheku2022-02-235-6/+70
| | | | | | | | | | | | | | | | * autoload.c (build_set_entries): Add oust symbol. * stdlib/build.tl (list-builder postinit): Call the self argument self instead of bc, for consistency with other methods. (list-builder oust): New method. (list-builder-flets): Add local function oust. * tests/012/seq.tl: New tests. * txr.1: Documented. * stdlib/doc-syms.tl: Updated.
* New function: partition-if.Kaz Kylheku2022-02-236-0/+199
| | | | | | | | | | | | | | | | * eval.c (eval_init): Register partition-if intrinsic. * lib.c (partition_if_countdown_funv, partition_if_func): New functions. (partition_if): New function. * lib.h (partition_if): Declared. * tests/012/seq.tl: New test cases. * txr.1: Documented. * stdlib/doc-syms.tl: Updated.
* find-max: convert to seq_info iteration.Kaz Kylheku2022-02-223-87/+36
| | | | | | | | | | | | * lib.c (find_max): Simplify into a single loop rather than handling various sequence types specially. This means it works for all iterable objects now. * txr.1: find-max documentation updated; discussion of hash tables removed, since the described behavior is the one expected for hash tables as iterables. * tests/012/seq.tl: Add some test coverage.
* gc: c++ fix in type_t conversion.Kaz Kylheku2022-02-222-3/+3
| | | | | | | | | | | | | | * gc.c (sweep_one): The recent fix to address the clang diagnostic from -fsanitize=implicit-conversion broke C++ compatibility, due to enums being type safe. We revert the expression to the original, before that fix, and address the clang diagnostic differently. * gc.h (REACHABLE, FREE): Add a U suffix to the constants to make them unsigned. The implicit conversion issue in the expression convert(type_t, block->t.type & ~REACHABLE) is that ~REACHABLE is -257, and is being converted to unsigned.
* termios: overflow in initialization on 32 bits.Kaz Kylheku2022-02-221-2/+2
| | | | | | | * termios.c (termios_init): The CMSPAR and CRTSCTS constants cannot be passed to num_fast; they are out of range, and so the corresponding cmspar and crtscts variables will end up with garbage values.
* New functions: find-max-key and find-min-key.Kaz Kylheku2022-02-215-0/+76
| | | | | | | | | | | | | * eval.c (eval_init): Register find-max-key and find-min-key intrinsics. * lib.c (find_max_key, find_min_key): New functions. * lib.h (find_max_key, find_min_key): Declared. * txr.1: Documented. * stdlib/doc-syms.tl: Updated.
* listener: restore and improve method completion.Kaz Kylheku2022-02-193-44/+7
| | | | | | | | | | | | | | | | | | | | | The recent autoload changes have degraded method completion, which didn't work well in the first place. Test case: with this change you can type term.(go[Tab] to complete to term.(go-raw or term.(go-cbreak. * parser.c (find_matching_syms): Do not use the get_slot_syms function for finding method and slot symbols. Just use get_visible_syms, like for other bindings. Instead, in the switch statement in the loop, for the 'M' (method) and 'S' (slot) completion types, we use the static_slot_types and slot_types functions to inquire whether a symbol has a binding. And these two functions do something important: unlike get_slot_syms, they trigger autoload. * struct.c (get_slot_syms): Function removed. * struct.h (get_slot_syms): Declaration removed.
* autoload: move socket material into socket module.Kaz Kylheku2022-02-194-72/+70
| | | | | | | | | | | | | | | | | * autoload.c (sock_set_entries, sock_instantiate): Functions removed from here. (autoload_init): autoload_reg call for above functions removed from here. (autoload_intern): New function, wrapper for intern_only. sock_set_entries uses intern_only which is static, so we need to expose this. * autoload.h (autoload_intern): Declared. * socket.c (sock_set_entries, sock_instantiate): Functions moved here. intern_only call replaced with autoload_intern. (sock_load_init): Function removed: body moved into sock_instantiate, which no longer calls sock_load_init.
* autoload: move termios material into termios module.Kaz Kylheku2022-02-192-25/+22
| | | | | | | | | | | * autoload.c (termios_set_entries, termios_instantiate): Static functions removed from here. (autoload_init): autoload_reg call for above functions removed from here. * termios.c (termios_set_entries, termios_instantiate): Static functions moved here. (termios_init): autoload_reg call moved here.
* autoload: use weak hash to erase registrations.Kaz Kylheku2022-02-194-99/+69
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | In autoload the _set_entries functions are being called twice: the first time with a functional argument to associate symbols with that function, and then when the module is loaded, a second time with a nil argument to erase those associations. This is inefficient; some modules have many symbols to remove from a hash. What we can do is make the hash weak; associate the symbols with a function weakly, such that those entries lapse when the function becomes garbage. Lapsing of weak hash table entries during GC is more efficient than making numerous calls to remhash. We can use a separate strong hash table to hang on to the functions; when a module is instantiated, all we have to do is remove the instantiation function from the table. Then that function is reachable only as a weak value from the other tables. * autoload.c (autoload_reg_hash): New static variable. (place_instantiate, ver_instantiate, ifa_instantiate, txr_case_instantiate, with_resources_instantiate, path_test_instantiate, struct_instantiate, with_stream_instantiate, hash_instantiate, except_instantiate, type_instantiate, yield_instantiate, sock_instantiate, termios_instantiate, awk_instantiate, build_instantiate, trace_instantiate, getopts_instantiate, package_instantiate, getput_instantiate, tagbody_instantiate, pmac_instantiate, error_instantiate, keyparams_instantiate, ffi_instantiate, doloop_instantiate, stream_wrap_instantiate, asm_instantiate, compiler_instantiate, debugger_instantiate, op_instantiate, save_exe_instantiate, defset_instantiate, copy_file_instantiate, arith_each_instantiate, each_prod_instantiate, quips_instantiate, match_instantiate, doc_instantiate, pic_instantiate, constfun_instantiate): Remove set_fun argument and call to that function. (autoload_reg): Remove argument from instantiate function. Add the function to the autoload_reg_hash table, associated with the t symbol. (autoload_init_tables): Initialize autoload_reg_hash. Turn the existing symbol hashes into weak tables. Let's use weak or-semantics, so they don't hang onto uninterned symbols. (autoload_try): If a symbol resolves to a function, indicating a load might be required, we now must verify whether that is really the case by seeing whether the function exists in the autolod_reg_hash table. If so, we remove it from that table and do the load. * autoload.h (autoload_reg): Declaration updated. * gencadr.txr (cadr_register): Remove set_fun argument and call to that function. * cadr.c: Regenerated.
* doc: or-semantics misformatted.Kaz Kylheku2022-02-191-1/+1
| | | | | * txr.1: In description of make-hash, fix bungled typesetting of "or-semantics".
* lisplib: rename to autoload.Kaz Kylheku2022-02-186-16/+16
| | | | | | | | | | | | | | | | | | | | | | * Makefile (OBJS): rename lisplib.o to autoload.o. * lisplib.c: Renamed to autoload.c. (lisplib_init_tables): Renamed to autoload_init_tables. (lisplib_init): Renamed to autoload_init, and calls autoload_init_tables. (lisplib_try_load): Renamed to autoload_try. (autoload_try_fun, autoload_try_var, autloload_try_slot, autoload_try_struct, autoload_try_keyword): Follow rename. * lisplib.h: Renamed to autoload.h. (lisplib_init): Renamed to autoload_init. * eval.c: Include autoload.h. (eval_init): Follow rename of lisplib_init. * gencadr.txr: include "autoload.h" * cadr.c: Regenerated.
* lisplib: rename lisplib funtions to autoload prefix.Kaz Kylheku2022-02-185-42/+42
| | | | | | | | | | | | | | | | | | | | | | | | | | | | * lisplib.c (lisplib_init): Follow rename, and rename try-load-fun to autoload-try-fun. (lisplib_try_load_fun, lisplib_try_load_var, lisplib_try_load_fun_var, lisplib_try_load_slot, lisplib_try_load_struct, lisplib_try_load_keyword): Renamed from `lisplib_try_load_@kind` to `autoload_try_@kind`. * lisplib.h (lisplib_try_load_fun, lisplib_try_load_var, lisplib_try_load_fun_var, lisplib_try_load_slot, lisplib_try_load_struct, lisplib_try_load_keyword): Declarations renamed. * eval.c (lookup_global_var, lookup_sym_lisp1, lookup_fun, lookup_mac, lookup_symac, lookup_symac_lisp1, special_var_p, expand_param_macro, rt_defvar, rt_defv, op_defsymacro, rt_defsymacro, rt_defun, rt_defmacro, makunbound, fmakunbound, mmakunbound): Follow rename. * struct.c (make_struct_type, find_struct_type, lookup_slot_load, lookup_static_slot_load, lookup_static_slot_desc_load, slot_types, static_slot_types): Follow rename. * stdlib/place.tl (get-place-macro): Follow rename of try-load-fun to autoload-try-fun.
* socket: add missing autoload entries for sockopts.Kaz Kylheku2022-02-181-1/+11
| | | | | | * lisplib.c (sock_set_entries): Add autoload entries for a number of socket-relatd variables that were recently moved out of the FFI module (no part of which auto-loads) into socket.c.
* lisplib: split autoload entries into namespaces.Kaz Kylheku2022-02-184-243/+289
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Autoloads are now keyed to spearate namespaces for functions/macros, variables, structures, slots and a keyword namespace for autoloading miscellaneous things tied to keywords like parameter macros. There is some renaming going on as part of this. The "dlt" acronym is replaced by "autload". Some functions in lisplib.c were in an inconsistent order, and were reordered. Motivation: the one namespace autoload causes hidden problems in the standard library, due to inappropriate loads. This shows up particularly when then uncompiled library is used. Tests no longer execute as they should. Story time: when struct.tl is autloaded, it loads param.tl for parameter parsing, param.tl defines a structure that has an opt slot, and accesses it with an expression like me.opt. When this expression is expanded, the qref macro checks whether opt is a valid slot, and for that, autoload is triggered. But, oops, opt is a function in getopts.tl, Thus param.tl autoloads getopt.tl. Paul A. Patience recently introduced changes into getopt.tl that make use of pattern matching. So getopt.tl auto-loads match.tl. But match.tl uses structures. But, oops, that material is defined in struct.tl, which is in progress of being auto-loaded. Thus, a false circular dependency exists, caused by a check of the opt slot triggering the loading of the irrelevant getopts.tl module. These irrelevant loads will also slow down the compiling of the library also, as well as test cases when the library is not compiled: especially tests run under the GC torture test. * lisplib.c (dl_table): Global variable removed. (autoload_hash): New static array. (set_dlt_entries_impl): Renamed to autoload_set_impl. (autoload_set_impl): Takes al_ns_t namespace enum argument. Operates on the hash table indicated by that argument. (set_dlt_entries): Renamed to autoload_set. (set_dlt_entries_sys): Renamed to autoload_sys_set. (autoload_key_set): New static function. (place_set_entries, ver_set_entries, ifa_set_entries, txr_case_set_entries, with_resources_set_entries, path_test_set_entries, struct_set_entries, with_stream_set_entries, hash_set_entries, except_set_entries, type_set_entries, yield_set_entries, sock_set_entries, termios_set_entries, awk_set_entries, build_set_entries, trace_set_entries, getopts_set_entries, package_set_entries, getput_set_entries, tagbody_set_entries, pmac_set_entries, error_set_entries, keyparams_set_entries, ffi_set_entries, doloop_set_entries, stream_wrap_set_entries, asm_set_entries, compiler_set_entries, debugger_set_entries, op_set_entries, save_exe_set_entries, defset_set_entries, copy_file_set_entries, arith_each_set_entries, each_prod_set_entries, quips_set_entries, match_set_entries, doc_set_entries, pic_set_entries, constfun_set_entries): Separate name arrays into multipel namespaces as appropriate and register using approprate namespace enums. (dlt_register): Renamed to autoload_reg. (autoload_reg): dlt parameter removed. (lisplib_try_load): Take namespace enum argument, and check against the corresponding hash. (lisplib_try_load_fun, lisplib_try_load_var, lisplib_try_load_slot, lisblib_try_load_struct, lisplib_try_load_keyword): Pass appropriate enum value to lisplib_try_load to request the right namespace. (lisplib_try_load_fun_var): Call lisplib_try_load_fun, and if that does nothing, lisplib_try_load_var. (lisplib_init_tables): New function. (lisplib_init): References to dl_table removed. Call to dlt_register replaced with autoload_reg. * lisplib.h (dl_table): Declaration removed. (enum autoload_ns, al_ns_t): New enum. (set_dlt_entries, dlt_register): Declarations removed. (autoload_set, autoload_reg): Declared. * gencadr.txr (cadr_set_entries): Drop dlt parameter, call autoload_set instead of set_dlt_entries. (cadr_init): Call autoload_reg instead of dlt_register. * cadr.c: regenerated.
* lisplib: split lisplib_try_load into namespaces.Kaz Kylheku2022-02-185-29/+64
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * lisplib.c (lisplib_init): Change sys:try-load (only used by place.tl) to sys:try-load-fun, and register it to (lisplib_try_load): External function becomes static. (lisplib_try_load_fun, lisplib_try_load_var, lisplib_try_load_fun_var, lisplib_try_load_slot, lisplib_try_load_struct, lisplib_try_load_keyword): New functions. * lisplib.h (lisplib_try_load): Declaration removed. (lisplib_try_load): External function becomes static. (lisplib_try_load_fun, lisplib_try_load_var, lisplib_try_load_fun_var, lisplib_try_load_slot, lisplib_try_load_struct, lisplib_try_load_keyword): Declared. * eval.c (lookup_global_var, lookup_var, lookup_symac, lookup_symac_lisp1, rt_defvarl, rt_defv, op_defsymacro, rt_defsymacro, makunbound): Use lisplip_try_load_var. (lookup_fun, lookup_mac, rt_defun, rt_defmacro, fmakunbound, mmakunbound): Use lisplib_try_load_fun. (expand_param_macro): Use lisplib_try_load_keyword, which is a catch-all namespace for anything tied to keywords. * struct.c (make_struct_type, find_struct_type): Use lisplib_try_load_struct. (lookup_sloat_load, lookup_static_slot_load, lookup_static_slot_desc_load, slot_types, static_slot_types): Use lisplib_try_load_slot. * stdlib/place.tl (get-place-macro): Use try-load-fun, rather than try-load.
* compiler: autoload on sys:*in-compilation-unit*.Kaz Kylheku2022-02-171-1/+1
| | | | | | | | | | | | | The compiler module contains the with-compilation-unit macro; if that macro is used, it is auto-loaded. However, when a file that uses with-compilation-unit is compiled, the macro is gone; its expansion refers to the sys:*in-compilation-unit* which is defined in the compiler module also. However, there is no autoload set up for it. * lisplib.c (compiler_set_entries): Add sys:*in-compilation-unit* to the list of symbols that trigger autoloading.
* ffi: move socket stuff to socket module.Kaz Kylheku2022-02-175-108/+135
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * alloca.h (zalloca): Macro moved here from ffi.c; it's useful to any code that wants to do a zero-filled alloca, and socket.c needs it now. * ffi.c (HAVE_SOCKETS): All includes conditional on HAVE_SOCKETS removed. (zalloca): Macro removed; moved to alloca.h. (ffi_type_struct_checked, ffi_type_lookup): Static functions changed to external linkage. (ffi_type_size, ffi_type_put, ffi_type_get): New functions, used by external module that has incomplete definition of struct txr_ffi_type. (type_by_size): New static array, moved out of ffi_init_extra_types function. (ffi_type_by_size): New function. (ffi_init_extra_types): type_by_size array relocated to file scope. (sock_opt, sock_set_opt): Moved to socket.c, and adjusted to use newly developed external access to needed ffi mechanisms. (ffi_init): Numerous definitions related to sockets removed; these go to socket.c. * ffi.h (struct txr_ffi_type): Declared here now as incomplete type. (ffi_type_struct_checked, ffi_type_size, ffi_type_put, ffi_type_get, ffi_type_lookup, ffi_type_by_size): Declared. * lib.c (init): Call new function sock_init. * socket.c (sock_opt, sock_set_opt): New functions, moved from ffi.c, and slightly adapted to work with external interfaces exposed by ffi.c. (sock_init): New function. This performs unconditional initializations not keyed to the lazy loading lisplib.c mechanism. Here we create the socklen-t FFI type. FFI types lookup doesn't trigger lazy loading, so we do it this way; the alternative would be to introduce lazy load triggering to FFI type lookup, just for this one type. (sock_load_init): All the socket function and variable registrations move here from ffi_init.
* doc: extend fboundp dialect note.Kaz Kylheku2022-02-171-2/+10
| | | | | | * txr.1: Mention that ANSI CL's fboundp doesn't yield true for lambda expressions, and how that affects the example expression's fidelity.
* time: fix missing day-of-week and day-of-year.Kaz Kylheku2022-02-162-11/+29
| | | | | | | | | | | | | | | | | | | | The time code entirely neglects the tm_wday and tm_yday fields of struct tm; they are missing from the Lisp time structure, and not referenced anywhere. * time.c (wday_s, yday_s): New symbol variables. (tm_to_time_struct): Transfer tm_wday and tm_yday values to Lisp struct. (time_fields_to_tm): Take wday and yday parameters, convert these and store in the given struct tm. (time_struct_to_tm): Retrieve wday and yday slots, and pass these values to time_fields_to_tm. (make_time_impl): Pass nil for wday and yday arguments of time_fields_to_tm, which is OK since mktime doesn't use those. (time_init): Intern the wday and yday symbols. Add those slots to the time structure. * txr.1: Documented new slots.
* doc: fix name of unique's equality function.Paul A. Patience2022-02-151-2/+2
| | | | | | | * txr.1: The unique function defaults to using an :equal-based hash table, in which case it considers elements to be equal under the equal function, rather than the eql function. Correct this typo, and also adjust the spacing of the .mets line.
* configure: typos in help.Kaz Kylheku2022-02-141-1/+1
| | | | * configure: fix "Flas" and "librariews" typos.
* tests: test case for combining .tlo files.Kaz Kylheku2022-02-141-0/+27
| | | | * tests/018/combine-tlo.tl: New file.
* random: shift bug uncovered by ubsan.Kaz Kylheku2022-02-141-1/+3
| | | | | | * rand.c (random): When calculating the msb_rand_mask, avoid shifting a 32 bit value by 32 bits. In that case we want the mask to be 0xFFFFFFFF, so we shift by zero.
* tests: don't run tets/012/stack.tl if built with ubsan.Kaz Kylheku2022-02-142-0/+8
| | | | | | | | | * configure (have_ubsan): New variable. This is set to y in the ubsan test, if detected. (gen_config_make): Add have_ubsan variable to config.make. * Makefile (TESTS_OK): If have_ubsan is true, filter out the ../012/stack.ok target that calls for that test case.
* tree: fix array underruns found by ubsan.Kaz Kylheku2022-02-141-11/+17
| | | | | | | | | | | | | | | | | | | | | * tree.c (tn_find_low): In this function it may happen that we are looking for a low bound that is beyond the highest value in the tree. In this situation, tdi->lastnode is nil; the loop that walks back up the path will not find it, and underrun. Now the word before the path array is the tree_iter_state, which is zero. That zero word terminates the loop so things work by fluke. (tr_find_rebuild_scapegoat): Firstly, since in this function we pre-decrement ti->depth, we guard the whole thing by ti->depth > 0. This is just a precaution; the algorithm guarantees that a scapegoat will be found, so this will not recurse to the depth == 0 case. The main fix here is this: we sometimes call tr_rebuild in the case when the parent is the root node. In that case, there is no grandparent. The path is empty and we are accessing before the array to try to fetch the nonexistent grandparent. Again, by fluke, there is a zero there thanks to the tree_iter_state which turns into a null pointer that tells tr_rebuild that the node has no parent. The correct way is to check the depth and pass nil as the grandparent if ti->depth is zero.
* Fix more -fsanitize=implicit-conversion findings.Kaz Kylheku2022-02-143-4/+7
| | | | | | | | | | | | | | | | | | | | | * arith.c (highest_significant_bit): Bugfix: do not pass a negative value to highest_bit, where we will get then get the wrong idea about the number of significant bits in the value, since the __builtin_clz primitives will include the sign bit. We want to complement the all the bits, so that the sign bit will go to zero. We can do this arithmetically by taking the additive inverse (which is the two's complement (which is the complement plus one)) and subtracting one. (ash): Avoid left shifting a negative number in HAVE_UBSAN mode using the same trick as in num_fast. * ffi.c (ffi_swap_u16): Here the shift and or calculation is producing a value beyond 16 bits which we are relying on the implicit conversion back to uin16_t to trim away. We add the cast to uint16_t to make it explicit. * hash.c (equal_hash): Also handle the CHR and NUM cases here via c_u like in eql_hash and eq_hash.
* Fix some issues found by ubsan.Kaz Kylheku2022-02-143-24/+29
| | | | | | | | | | | | | | | | * lib.h (num_fast): Under HAVE_UBSAN, avoid left shifting a negative value, which is diagnosed by ubsan (by default), but is otherwise a documented GCC extension that behaves sanely in the manner one would expect on two's complement machines. * mpi/mpi.c (s_mp_mul_2d): Fix instance of undefined shift equal to the width of the type. This occurs in the case when d == 0. In that case, we try to calculate a 32 or 64 bit full mask by shifting 1 to the left that many times and then subtracting 1. However, the entire code for the fractional shift is not necessary in that case and may be skipped. I'm also removing the unnecesary s_mp_clamp call. If the number is clamped on entry into the function, it stays clamped.
* configure: detect ubsan compilation.Kaz Kylheku2022-02-141-0/+21
| | | | | | | | * configure: detect that the compiler is generating undefined behavior sanitizer code, a feature supported in GCC and Clang. The tell-tale sign of this is that "ubsan" occurs in the executable. If so, we deposit #define HAVE_UBSAN 1 into config.h.
* Another implicit conversion in continuations implementation.Kaz Kylheku2022-02-141-1/+1
| | | | | | * unwind.c (revive_cont): Explicitly convert delta, which is necessarily signed because it may be negative, to uint_ptr_t before adding it to that type.
* Few adjustments to no-implicit-conversion patch.Kaz Kylheku2022-02-143-15/+15
| | | | | | | | | | | | | | | | | | | | * lib.h (c_u): New inline function: unsafe conversion to ucnum, analogous to c_n for cnum. * hash.c (equal_hash, hash_iter_init): Use UINT_PTR_MAX instead of convert(ucnum, -1). (eql_hash): mp_hash returns unsigned long, so shouldn't require a cast to go to the uint_ptr_t. The types are of the same size, or at worst it is a widening. Also replace convert(ucnum, -1) by UINT_PTR_MAX here. Combining the TAG_CHR and TAG_NUM cases, and using c_u, which is more efficient since c_chr and c_num are non-inlined functions which redundantly check type. We no longer need a self variable in this function. (eq_hash): Same TAG_CHR and TAG_NUM changes as eql_hash. * regex.c (char_set_add): Reformat change to avoid line break across assignment.
* Fix various instances of implicit conversions.Paul A. Patience2022-02-146-13/+14
| | | | | | | | | | | | | | | | | | | | | The implicit conversions were discovered with Clang's UBSan (with the -fsanitizer=implicit-conversion option). * gc.c (sweep_one): Convert only the inverted REACHABLE, since block->t.type is already of the right type. * hash.c (eql_hash, eq_hash, hash_iter_init, us_hash_iter_init): Explicitly convert to ucnum. * linenoise/linenoise.c (enable_raw_mode): Explicitly convert the inverted flag sets to tcflag_t. * mpi/mpi.c (mp_set_uintptr): Explicitly convert to uint_ptr_t. * regex.c (char_set_add): Explicitly convert to bitcell_t. * struct.c (struct_inst_hash): Correct type of hash from cnum to ucnum.
* sockets: appease -Wint-in-bool-context warning.Paul A. Patience2022-02-131-2/+2
| | | | | | * socket.c (open_socket, socketpair_wrap): Replace logical OR of SOCK_NONBLOCK and SOCK_CLOEXEC with bitwise OR in order to silence Clang 13's -Wint-in-bool-context warning.
* doc: document catenated .tlo files.Kaz Kylheku2022-02-131-0/+35
| | | | | * txr.1: Under load and compile-file, mention the support for loading catenated .tlo files.
* New function: cat-files.Kaz Kylheku2022-02-134-1/+35
| | | | | | | | | | | * lisplib.c (copy_file_instantiate): Trigger autoload on cat-files. * stdlib/copy-file.tl (cat-files): New function. * txr.1: Documented. * stdlib/doc-syms.tl: Updated.