mirror of
https://github.com/iustin/mt-st.git
synced 2025-12-23 05:45:13 +00:00
Check for overflow when using k, M or G
Check that using a postfix (k, M, G) with repeat count does not cause overflow in the int mtop.mt_count.
This commit is contained in:
19
mt.c
19
mt.c
@@ -16,6 +16,7 @@
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/stat.h>
|
||||
@@ -444,18 +445,30 @@ usage(int explain)
|
||||
static int
|
||||
do_standard(int mtfd, cmdef_tr *cmd, int argc, char **argv)
|
||||
{
|
||||
int multiplier, max_count;
|
||||
struct mtop mt_com;
|
||||
char *endp;
|
||||
|
||||
mt_com.mt_op = cmd->cmd_code;
|
||||
mt_com.mt_count = (argc > 0 ? strtol(*argv, &endp, 0) : 1);
|
||||
if (argc > 0 && endp != *argv) {
|
||||
multiplier = 1;
|
||||
if (*endp == 'k')
|
||||
mt_com.mt_count *= 1024;
|
||||
multiplier = 1024;
|
||||
else if (*endp == 'M')
|
||||
mt_com.mt_count *= 1024 * 1024;
|
||||
multiplier = 1024 * 1024;
|
||||
else if (*endp == 'G')
|
||||
mt_com.mt_count *= 1024 * 1024 * 1024;
|
||||
multiplier = 1024 * 1024 * 1024;
|
||||
else if (*endp != 0) {
|
||||
fprintf(stderr, "mt: illegal count unit.\n");
|
||||
return 3;
|
||||
}
|
||||
max_count = INT_MAX / multiplier;
|
||||
if (mt_com.mt_count > max_count) {
|
||||
fprintf(stderr, "mt: repeat count too large.\n");
|
||||
return 3;
|
||||
}
|
||||
mt_com.mt_count *= multiplier;
|
||||
}
|
||||
mt_com.mt_count |= cmd->cmd_count_bits;
|
||||
if (mt_com.mt_count < 0) {
|
||||
|
||||
Reference in New Issue
Block a user