Index: head/sys/kern/kern_resource.c =================================================================== --- head/sys/kern/kern_resource.c +++ head/sys/kern/kern_resource.c @@ -1384,18 +1384,17 @@ static inline int chglimit(struct uidinfo *uip, long *limit, int diff, rlim_t max, const char *name) { + long new; /* Don't allow them to exceed max, but allow subtraction. */ + new = atomic_fetchadd_long(limit, (long)diff) + diff; if (diff > 0 && max != 0) { - if (atomic_fetchadd_long(limit, (long)diff) + diff > max) { + if (new < 0 || new > max) { atomic_subtract_long(limit, (long)diff); return (0); } - } else { - atomic_add_long(limit, (long)diff); - if (*limit < 0) - printf("negative %s for uid = %d\n", name, uip->ui_uid); - } + } else if (new < 0) + printf("negative %s for uid = %d\n", name, uip->ui_uid); return (1); }