diff --git a/ChangeLog b/ChangeLog index 02832f48ae..d51ea17082 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +Fri Feb 15 00:15:31 2013 Tanaka Akira + + * ext/socket/sockport.h (VALIDATE_SOCKLEN): new macro to validate + sa_len member of 4.4BSD socket address. + + * ext/socket/getnameinfo.c (getnameinfo): use VALIDATE_SOCKLEN, + instead of SA_LEN. + + * ext/socket/socket.c (sock_s_getnameinfo): use VALIDATE_SOCKLEN + instead of SS_LEN. + Thu Feb 14 22:25:54 2013 Tanaka Akira * ext/socket/socket.c (sockaddr_len): extracted from sockaddr_obj. diff --git a/ext/socket/getnameinfo.c b/ext/socket/getnameinfo.c index d1d5ff6c73..d0c17e1826 100644 --- a/ext/socket/getnameinfo.c +++ b/ext/socket/getnameinfo.c @@ -151,8 +151,8 @@ getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, socklen_t ho if (sa == NULL) return ENI_NOSOCKET; - len = SA_LEN(sa); - if (len != salen) return ENI_SALEN; + if (!VALIDATE_SOCKLEN(sa, salen)) return ENI_SALEN; + len = salen; family = sa->sa_family; for (i = 0; afdl[i].a_af; i++) diff --git a/ext/socket/socket.c b/ext/socket/socket.c index bae6bf288c..be3f8cc4f1 100644 --- a/ext/socket/socket.c +++ b/ext/socket/socket.c @@ -1235,7 +1235,7 @@ sock_s_getnameinfo(int argc, VALUE *argv) rb_raise(rb_eTypeError, "sockaddr length too big"); } memcpy(&ss, RSTRING_PTR(sa), RSTRING_LEN(sa)); - if ((size_t)RSTRING_LEN(sa) != SS_LEN(&ss)) { + if (!VALIDATE_SOCKLEN((struct sockaddr *)&ss, RSTRING_LEN(sa))) { rb_raise(rb_eTypeError, "sockaddr size differs - should not happen"); } sap = (struct sockaddr*)&ss; diff --git a/ext/socket/sockport.h b/ext/socket/sockport.h index 5ab087d97b..43784fca61 100644 --- a/ext/socket/sockport.h +++ b/ext/socket/sockport.h @@ -10,6 +10,12 @@ #ifndef SOCKPORT_H #define SOCKPORT_H +#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN +# define VALIDATE_SOCKLEN(addr, len) ((addr)->sa_len == (len)) +#else +# define VALIDATE_SOCKLEN(addr, len) ((void)(addr), (void)(len), 1) +#endif + #ifdef SA_LEN # define SS_LEN(ss) (ss)->ss_len #else