Skip to contentSkip to conversation

shell: Fix tst_get_unused_port endianity

PATCH New Petr Vorel 2 participants 3 messages Jun 6, 2019 – Jun 11, 2019

lore
Share & tools
lore

Petr Vorel

author
#0 lore raw

PATCH 1/1 shell: Fix tst_get_unused_port endianity

tst_get_unused_port is a shell helper, therefore it should use host byte order (we're not expecting this value would be added directly to sockaddr_in structure, which require network byte order). Wrap TST_GET_UNUSED_PORT() with ntohs() to achieve it.

---
Hi,

I guess we didn't noticed this in shell script as sendfile01 is the only
one not requiring root (dns-stress, tst_net_stress.sh, ssh-stress, ssh-stress03-rmt do require it).

I tested patch with little script (could be easily converted to proper test placed in lib/newlib_tests/)

export PATH="/opt/ltp/testcases/bin/:$PATH"
i=0
while [ $i -lt 2048 ]; do

    for j in ipv4 ipv6; do
        for k in stream dgram; do
            p="$(tst_get_unused_port $j $k)"
            printf "$i ($j $k): $p "
            if [ $p -lt 1024 ]; then
                echo "(bad)"
                exit 1
            else
                echo "(ok)"
            fi
        done
    done
    i=$((i+1))

done
echo "Test was ok"

 testcases/lib/tst_get_unused_port.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
testcases/lib/tst_get_unused_port.c +1 -1
index 0173f1634..a843cc6e3 100644
@@ -54,6 +54,6 @@ int main(int argc, char *argv[])
 		return 1;
 	}
 
-	printf("%d", TST_GET_UNUSED_PORT(family, type));
+	printf("%d", ntohs(TST_GET_UNUSED_PORT(family, type)));
 	return 0;
 }
Signature · 1 line
2.21.0
#1 lore raw

PATCH 1/1 shell: Fix tst_get_unused_port endianity

Quoted 56 lines · Petr Vorel

tst_get_unused_port is a shell helper, therefore it should use host byte order (we're not expecting this value would be added directly to sockaddr_in structure, which require network byte order). Wrap TST_GET_UNUSED_PORT() with ntohs() to achieve it.

---
Hi,

I guess we didn't noticed this in shell script as sendfile01 is the only
one not requiring root (dns-stress, tst_net_stress.sh, ssh-stress, ssh-stress03-rmt do require it).

I tested patch with little script (could be easily converted to proper test placed in lib/newlib_tests/)

export PATH="/opt/ltp/testcases/bin/:$PATH"
i=0
while [ $i -lt 2048 ]; do
for j in ipv4 ipv6; do

       for k in stream dgram; do
           p="$(tst_get_unused_port $j $k)"
           printf "$i ($j $k): $p "
           if [ $p -lt 1024 ]; then
               echo "(bad)"
               exit 1
           else
               echo "(ok)"
           fi
       done

done
i=$((i+1))
done
echo "Test was ok"

testcases/lib/tst_get_unused_port.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
testcases/lib/tst_get_unused_port.c +1 -1
index 0173f1634..a843cc6e3 100644
@@ -54,6 +54,6 @@ int main(int argc, char *argv[])
 		return 1;
 	}
 
-	printf("%d", TST_GET_UNUSED_PORT(family, type));
+	printf("%d", ntohs(TST_GET_UNUSED_PORT(family, type)));
 	return 0;
 }
Signature · 1 line
2.21.0
Reviewed-by Enji Cooper <[email protected] <mailto:[email protected]>>

Thanks!
-Enji
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20190606/b78a0519/attachment.html>