Issue #18560: Fix potential NULL pointer dereference in sum()