summaryrefslogtreecommitdiff
path: root/libguile/sort.c
diff options
context:
space:
mode:
authorAndy Wingo <wingo@pobox.com>2017-04-17 11:26:17 +0200
committerAndy Wingo <wingo@pobox.com>2017-04-18 21:27:45 +0200
commit6e573a0885d24d9ed36141ddf561c8b8b2e288e9 (patch)
treef3a6b348517b69856194efa2f0fcbec79286c4e6 /libguile/sort.c
parentd7778b3d6a5f11ef4744c80e70457193d672aeda (diff)
downloadguile-6e573a0885d24d9ed36141ddf561c8b8b2e288e9.tar.gz
Attempt to mutate residualized literal pair throws exception
* libguile/validate.h (SCM_VALIDATE_MUTABLE_PAIR): * libguile/pairs.h (scm_is_mutable_pair): New internal definitions. * libguile/pairs.c (scm_set_car_x, scm_set_cdr_x): Validate mutable pairs. * libguile/alist.c (scm_assq_set_x, scm_assv_set_x, scm_assoc_set_x): * libguile/list.c (scm_reverse_x, scm_list_set_x, scm_list_cdr_set_x): * libguile/srcprop.c (scm_make_srcprops): * libguile/srfi-1.c (scm_srfi1_append_reverse_x) (scm_srfi1_delete_duplicates_x): * libguile/symbols.c (scm_symbol_fset_x, scm_symbol_pset_x): * libguile/sort.c (scm_merge_list_x): Use scm_set_car_x / scm_set_cdr_x instead of the macros, so as to check for mutable pairs. (SCM_VALIDATE_MUTABLE_LIST): New internal helper macro. (scm_sort_x, scm_stable_sort_x, scm_sort_list_x): Use SCM_VALIDATE_MUTABLE_LIST. * libguile/vm-engine.c (VM_VALIDATE_MUTABLE_PAIR): New definition. (set-car!, set-cdr!): Use VM_VALIDATE_MUTABLE_PAIR. Fix error message for set-cdr!.
Diffstat (limited to 'libguile/sort.c')
-rw-r--r--libguile/sort.c20
1 files changed, 16 insertions, 4 deletions
diff --git a/libguile/sort.c b/libguile/sort.c
index 8c20d3453..81ef3ff27 100644
--- a/libguile/sort.c
+++ b/libguile/sort.c
@@ -306,22 +306,22 @@ scm_merge_list_x (SCM alist, SCM blist,
SCM_TICK;
if (scm_is_true (scm_call_2 (less, SCM_CAR (blist), SCM_CAR (alist))))
{
- SCM_SETCDR (last, blist);
+ scm_set_cdr_x (last, blist);
blist = SCM_CDR (blist);
blen--;
}
else
{
- SCM_SETCDR (last, alist);
+ scm_set_cdr_x (last, alist);
alist = SCM_CDR (alist);
alen--;
}
last = SCM_CDR (last);
}
if ((alen > 0) && (blen == 0))
- SCM_SETCDR (last, alist);
+ scm_set_cdr_x (last, alist);
else if ((alen == 0) && (blen > 0))
- SCM_SETCDR (last, blist);
+ scm_set_cdr_x (last, blist);
}
return build;
} /* scm_merge_list_x */
@@ -398,6 +398,14 @@ scm_merge_list_step (SCM * seq, SCM less, long n)
} /* scm_merge_list_step */
+#define SCM_VALIDATE_MUTABLE_LIST(pos, lst) \
+ do { \
+ SCM walk; \
+ for (walk = lst; !scm_is_null_or_nil (walk); walk = SCM_CDR (walk)) \
+ SCM_VALIDATE_MUTABLE_PAIR (pos, walk); \
+ } while (0)
+
+
SCM_DEFINE (scm_sort_x, "sort!", 2, 0, 0,
(SCM items, SCM less),
"Sort the sequence @var{items}, which may be a list or a\n"
@@ -414,6 +422,7 @@ SCM_DEFINE (scm_sort_x, "sort!", 2, 0, 0,
if (scm_is_pair (items))
{
SCM_VALIDATE_LIST_COPYLEN (1, items, len);
+ SCM_VALIDATE_MUTABLE_LIST (1, items);
return scm_merge_list_step (&items, less, len);
}
else if (scm_is_array (items) && scm_c_array_rank (items) == 1)
@@ -533,6 +542,7 @@ SCM_DEFINE (scm_stable_sort_x, "stable-sort!", 2, 0, 0,
if (scm_is_pair (items))
{
SCM_VALIDATE_LIST_COPYLEN (1, items, len);
+ SCM_VALIDATE_MUTABLE_LIST (1, items);
return scm_merge_list_step (&items, less, len);
}
else if (scm_is_array (items) && 1 == scm_c_array_rank (items))
@@ -596,6 +606,8 @@ SCM_DEFINE (scm_sort_list_x, "sort-list!", 2, 0, 0,
long len;
SCM_VALIDATE_LIST_COPYLEN (1, items, len);
+ SCM_VALIDATE_MUTABLE_LIST (1, items);
+
return scm_merge_list_step (&items, less, len);
}
#undef FUNC_NAME