From 0a0c8e010d4672cfeb0b980d7c23846e8744d2d3 Mon Sep 17 00:00:00 2001 From: Eitan Adler Date: Sun, 4 Mar 2012 16:39:08 +0000 Subject: [PATCH] POSIX mandates that swab do nothing when len < 0 PR: kern/140690 Submitted by: Jeremy Huddleston Approved by: cperciva MFC after: 2 weeks --- lib/libc/string/swab.3 | 5 ++++- lib/libc/string/swab.c | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/libc/string/swab.3 b/lib/libc/string/swab.3 index f8ef34ee3f3b..0c9deea8463b 100644 --- a/lib/libc/string/swab.3 +++ b/lib/libc/string/swab.3 @@ -28,7 +28,7 @@ .\" @(#)swab.3 8.1 (Berkeley) 6/4/93 .\" $FreeBSD$ .\" -.Dd December 10, 2004 +.Dd March 4, 2012 .Dt SWAB 3 .Os .Sh NAME @@ -54,6 +54,9 @@ swapping adjacent bytes. The argument .Fa len must be an even number. +If +.Fa len +is less than zero, nothing will be done. .Sh SEE ALSO .Xr bzero 3 , .Xr memset 3 diff --git a/lib/libc/string/swab.c b/lib/libc/string/swab.c index 1db7d1fab00b..ec6cbf0f536f 100644 --- a/lib/libc/string/swab.c +++ b/lib/libc/string/swab.c @@ -45,6 +45,8 @@ swab(const void * __restrict from, void * __restrict to, ssize_t len) int n; char *fp, *tp; + if (len <= 0) + return; n = len >> 1; fp = (char *)from; tp = (char *)to;