Merge release-5-0 into master
[alexxy/gromacs.git] / src / gromacs / gmxpreprocess / pdb2gmx.c
index 95215c89f6653778caa8fdd8d0ef666a3bb0a059..242aaf65e08ec587661d8acc684dcaaccd5348cb 100644 (file)
 #include "gromacs/utility/fatalerror.h"
 #include "gromacs/utility/smalloc.h"
 
+#define RTP_MAXCHAR 5
 typedef struct {
-    char gmx[6];
-    char main[6];
-    char nter[6];
-    char cter[6];
-    char bter[6];
+    char gmx[RTP_MAXCHAR+2];
+    char main[RTP_MAXCHAR+2];
+    char nter[RTP_MAXCHAR+2];
+    char cter[RTP_MAXCHAR+2];
+    char bter[RTP_MAXCHAR+2];
 } rtprename_t;
 
 
@@ -221,8 +222,25 @@ static void read_rtprename(const char *fname, FILE *fp,
     while (get_a_line(fp, line, STRLEN))
     {
         srenew(rr, n+1);
-        nc = sscanf(line, "%s %s %s %s %s %s",
-                    rr[n].gmx, rr[n].main, rr[n].nter, rr[n].cter, rr[n].bter, buf);
+        /* line is NULL-terminated and length<STRLEN, so final arg cannot overflow.
+         * For other args, we read up to 6 chars (so we can detect if the length is > 5).
+         * Note that the buffer length has been increased to 7 to allow this,
+         * so we just need to make sure the strings have zero-length initially.
+         */
+        rr[n].gmx[0]  = '\0';
+        rr[n].main[0] = '\0';
+        rr[n].nter[0] = '\0';
+        rr[n].cter[0] = '\0';
+        rr[n].bter[0] = '\0';
+        nc            = sscanf(line, "%6s %6s %6s %6s %6s %s",
+                               rr[n].gmx, rr[n].main, rr[n].nter, rr[n].cter, rr[n].bter, buf);
+        if (strlen(rr[n].gmx) > RTP_MAXCHAR || strlen(rr[n].main) > RTP_MAXCHAR ||
+            strlen(rr[n].nter) > RTP_MAXCHAR || strlen(rr[n].cter) > RTP_MAXCHAR || strlen(rr[n].bter) > RTP_MAXCHAR)
+        {
+            gmx_fatal(FARGS, "Residue renaming database '%s' has strings longer than %d chars in first 5 columns:\n%s",
+                      fname, RTP_MAXCHAR, line);
+        }
+
         if (ncol == 0)
         {
             if (nc != 2 && nc != 5)