From 44e48457cd329fe0a467c5bf9a8b8d514c8e6e0d Mon Sep 17 00:00:00 2001 From: Andre Pinto Date: Thu, 4 Dec 2014 11:52:53 -0800 Subject: [PATCH] Correct getsockopt call made by AsyncSocket Summary: The getsockopt's last parameter (optlen) is a value-result parameter and AsyncSocket::getSockOpt was passing a value as argument. Test Plan: Unit tests Reviewed By: alikhtarov@fb.com Subscribers: trunkagent, njormrod, folly-diffs@ FB internal diff: D1717463 Tasks: 4867290 Signature: t1:1717463:1417664828:6c7a74ff31725121f892ce1adba2653e70728192 --- folly/io/async/AsyncSocket.h | 7 +++-- folly/io/async/test/AsyncSocketTest.cpp | 36 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 folly/io/async/test/AsyncSocketTest.cpp diff --git a/folly/io/async/AsyncSocket.h b/folly/io/async/AsyncSocket.h index f8eb8e55..40560660 100644 --- a/folly/io/async/AsyncSocket.h +++ b/folly/io/async/AsyncSocket.h @@ -530,11 +530,14 @@ class AsyncSocket : virtual public AsyncTransport { * @param optname same as the "optname" parameter in getsockopt(). * @param optval pointer to the variable in which the option value should * be returned. + * @param optlen value-result argument, initially containing the size of + * the buffer pointed to by optval, and modified on return + * to indicate the actual size of the value returned. * @return same as the return value of getsockopt(). */ template - int getSockOpt(int level, int optname, T *optval) { - return getsockopt(fd_, level, optname, optval, sizeof(T)); + int getSockOpt(int level, int optname, T* optval, socklen_t* optlen) { + return getsockopt(fd_, level, optname, (void*) optval, optlen); } /** diff --git a/folly/io/async/test/AsyncSocketTest.cpp b/folly/io/async/test/AsyncSocketTest.cpp new file mode 100644 index 00000000..473c87ec --- /dev/null +++ b/folly/io/async/test/AsyncSocketTest.cpp @@ -0,0 +1,36 @@ +/* + * Copyright 2014 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include + +#include +#include + +#include + +TEST(AsyncSocketTest, getSockOpt) { + folly::EventBase evb; + std::shared_ptr socket = + folly::AsyncSocket::newSocket(&evb, 0); + + int val; + socklen_t len; + + int expectedRc = getsockopt(socket->getFd(), SOL_SOCKET, + SO_REUSEADDR, &val, &len); + int actualRc = socket->getSockOpt(SOL_SOCKET, SO_REUSEADDR, &val, &len); + + EXPECT_EQ(expectedRc, actualRc); +} -- 2.34.1