File bsc1063910-use-ulong-for-inode.patch of Package net-tools.40435

Upstream ref: https://github.com/ecki/net-tools/commit/74b2b5487338d65c468f76a7530c8

netstat: fix handling of large socket numbers

The kernel really only treats sockets with a value of -1 as an error.
The rest are an unsigned quantity.  So tweak the nestat lookup to use
an unsigned value.

patch based on Antonio Galea's work in Debian bug 345331
---

Index: netstat.c
===================================================================
--- netstat.c.orig
+++ netstat.c
@@ -415,15 +415,15 @@ static void prg_cache_clear(void)
     prg_cache_loaded=0;
 }
 
-static void extract_type_1_socket_inode(const char lname[], long * inode_p) {
+static int extract_type_1_socket_inode(const char lname[], unsigned long * inode_p) {
 
     /* If lname is of the form "socket:[12345]", extract the "12345"
-       as *inode_p.  Otherwise, return -1 as *inode_p.
+       as *inode_p and return 0.  Otherwise, return -1.
        */
 
-    if (strlen(lname) < PRG_SOCKET_PFXl+3) *inode_p = -1;
-    else if (memcmp(lname, PRG_SOCKET_PFX, PRG_SOCKET_PFXl)) *inode_p = -1;
-    else if (lname[strlen(lname)-1] != ']') *inode_p = -1;
+    if (strlen(lname) < PRG_SOCKET_PFXl+3) return -1;
+    else if (memcmp(lname, PRG_SOCKET_PFX, PRG_SOCKET_PFXl)) return -1;
+    else if (lname[strlen(lname)-1] != ']') return -1;
     else {
         char inode_str[strlen(lname + 1)];  /* e.g. "12345" */
         const int inode_str_len = strlen(lname) - PRG_SOCKET_PFXl - 1;
@@ -431,29 +431,33 @@ static void extract_type_1_socket_inode(
 
         strncpy(inode_str, lname+PRG_SOCKET_PFXl, inode_str_len);
         inode_str[inode_str_len] = '\0';
-        *inode_p = strtol(inode_str,&serr,0);
-        if (!serr || *serr || *inode_p < 0 || *inode_p >= INT_MAX) 
-            *inode_p = -1;
+        *inode_p = strtoul(inode_str,&serr,0);
+        if (!serr || *serr || *inode_p == ~0) 
+            return -1;
     }
+    
+    return 0;
 }
 
 
 
-static void extract_type_2_socket_inode(const char lname[], long * inode_p) {
+static int extract_type_2_socket_inode(const char lname[], unsigned long * inode_p) {
 
     /* If lname is of the form "[0000]:12345", extract the "12345"
-       as *inode_p.  Otherwise, return -1 as *inode_p.
+       as *inode_p and return 0.  Otherwise, return -1.
        */
 
-    if (strlen(lname) < PRG_SOCKET_PFX2l+1) *inode_p = -1;
-    else if (memcmp(lname, PRG_SOCKET_PFX2, PRG_SOCKET_PFX2l)) *inode_p = -1;
+    if (strlen(lname) < PRG_SOCKET_PFX2l+1) return -1;
+    else if (memcmp(lname, PRG_SOCKET_PFX2, PRG_SOCKET_PFX2l)) return -1;
     else {
         char *serr;
 
-        *inode_p=strtol(lname + PRG_SOCKET_PFX2l,&serr,0);
-        if (!serr || *serr || *inode_p < 0 || *inode_p >= INT_MAX) 
-            *inode_p = -1;
+        *inode_p=strtoul(lname + PRG_SOCKET_PFX2l,&serr,0);
+        if (!serr || *serr || *inode_p == ~0) 
+            return -1;
     }
+
+    return 0;
 }
 
 
@@ -463,7 +467,7 @@ static void prg_cache_load(void)
     char line[LINE_MAX],eacces=0;
     int procfdlen,fd,cmdllen,lnamelen;
     char lname[30],cmdlbuf[512],finbuf[PROGNAME_WIDTH];
-    long inode;
+    unsigned long inode;
     const char *cs,*cmdlp;
     DIR *dirproc=NULL,*dirfd=NULL;
     struct dirent *direproc,*direfd;
@@ -506,11 +510,9 @@ static void prg_cache_load(void)
 	    lnamelen=readlink(line,lname,sizeof(lname)-1);
             lname[lnamelen] = '\0';  /*make it a null-terminated string*/
 
-            extract_type_1_socket_inode(lname, &inode);
-
-            if (inode < 0) extract_type_2_socket_inode(lname, &inode);
-
-            if (inode < 0) continue;
+            if (extract_type_1_socket_inode(lname, &inode) < 0 &&
+                extract_type_2_socket_inode(lname, &inode) < 0)
+              continue;
 
 	    if (!cmdlp) {
 		if (procfdlen - PATH_FD_SUFFl + PATH_CMDLINEl >= 
@@ -650,7 +652,7 @@ static void finish_this_one(int uid, uns
 	    printf("%-10s ", pw->pw_name);
 	else
 	    printf("%-10d ", uid);
-	printf("%-10ld ",inode);
+	printf("%-10lu ",inode);
     }
     if (flag_prg)
 	printf("%-" PROGNAME_WIDTHs "s",prg_cache_get(inode));
@@ -839,7 +841,7 @@ static void tcp_do_one(int lnr, const ch
 	return;
 
     num = sscanf(line,
-    "%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %ld %512s\n",
+    "%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %lu %512s\n",
 		 &d, local_addr, &local_port, rem_addr, &rem_port, &state,
 		 &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more);
 
@@ -968,7 +970,7 @@ static void udp_do_one(int lnr, const ch
 
     more[0] = '\0';
     num = sscanf(line,
-		 "%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %ld %512s\n",
+		 "%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %lu %512s\n",
 		 &d, local_addr, &local_port,
 		 rem_addr, &rem_port, &state,
 	  &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more);
@@ -1463,7 +1465,7 @@ static void raw_do_one(int lnr, const ch
 
     more[0] = '\0';
     num = sscanf(line,
-		 "%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %ld %512s\n",
+		 "%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %lu %512s\n",
 		 &d, local_addr, &local_port, rem_addr, &rem_port, &state,
 	  &txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more);
 
@@ -1588,14 +1590,14 @@ static void unix_do_one(int nr, const ch
 	return;
     }
     path[0] = '\0';
-    num = sscanf(line, "%p: %lX %lX %lX %X %X %ld %s",
+    num = sscanf(line, "%p: %lX %lX %lX %X %X %lu %s",
 		 &d, &refcnt, &proto, &flags, &type, &state, &inode, path);
     if (num < 6) {
 	fprintf(stderr, _("warning, got bogus unix line.\n"));
 	return;
     }
     if (!(has & HAS_INODE))
-	snprintf(path,sizeof(path),"%ld",inode);
+	snprintf(path,sizeof(path),"%lu",inode);
 
     if (!flag_all) {
     	if ((state == SS_UNCONNECTED) && (flags & SO_ACCEPTCON)) {
@@ -1687,7 +1689,7 @@ static void unix_do_one(int nr, const ch
     printf("%-5s %-6ld %-11s %-10s %-13s ",
 	   ss_proto, refcnt, ss_flags, ss_type, ss_state);
     if (has & HAS_INODE)
-	printf("%-6ld ",inode);
+	printf("%-6lu ",inode);
     else
 	printf("-      ");
     if (flag_prg)
openSUSE Build Service is sponsored by