device_name = optarg;
                        break;
                case 'o':
-                       lines[i] = strtoul(optarg, NULL, 10);
+                       /*
+                        * Avoid overflow. Do not immediately error, we want to
+                        * be able to accurately report on the amount of times
+                        * '-o' was given to give an accurate error message
+                        */
+                       if (i < GPIOHANDLES_MAX)
+                               lines[i] = strtoul(optarg, NULL, 10);
+
                        i++;
                        break;
                case '?':
                        return -1;
                }
        }
+
+       if (i >= GPIOHANDLES_MAX) {
+               fprintf(stderr,
+                       "Only %d occurences of '-o' are allowed, %d were found\n",
+                       GPIOHANDLES_MAX, i + 1);
+               return -1;
+       }
+
        nlines = i;
 
        if (!device_name || !nlines) {