-1

Getting this error when performing make:

$ make
cc -O2 -g src/bittwist.c -o src/bittwist -I/usr/local/include -L/usr/local/lib -lpcap
cc -O2 -g src/bittwiste.c -o src/bittwiste -I/usr/local/include -L/usr/local/lib -lpcap
src/bittwiste.c: In function ‘main’:
src/bittwiste.c:99:21: warning: format ‘%x’ expects argument of type ‘unsigned int *’, but argument 3 has type ‘u_char *’ [-Wformat=]
                     sscanf(optarg, "%02x", &payload_opt[i]);
                     ^

Here is the code:

payload_len_opt = (u_short)c / 2; / possible resizing in editing functions /
payload_opt = (u_char *)malloc(sizeof(u_char) *payload_len_opt);
if (payload_opt == NULL)
error("malloc(): cannot allocate memory for payload_opt"); / make a byte of data from every 2 characters of optarg /
for (i = 0; i < payload_len_opt; i++) {
/ ugly - let me know if there is a better way to achieve this /
sscanf(optarg, "%02x", &payload_opt[i]);

I'm using Ubuntu 14.04.2 LTS

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Mindaugas Bernatavičius
  • 3,757
  • 4
  • 31
  • 58

1 Answers1

3

First, in your code

payload_opt = (u_char )malloc(sizeof(u_char)  payload_len_opt);

looks very wrong, because

  1. Standard Warning : Please do not cast the return value of malloc() and family in C. If u_char is not of a pointer type, you're into deep trouble. So, better, don't cast at all. It is not at all required.

  2. Even in case, your u_char is a pointer type, maybe a typedef to unsigned char *, you're missing a multiplication (*) operator there. in c, sizeof(char) is guaranteed to be 1, so you can just write

    payload_opt = malloc(payload_len_opt);
    
  3. The multiline comment syntax is /*...*/, not / ... / (missing *)

That said, regarding the error message, for %x format specifier, from C11, chapter §7.21.6.2

....The corresponding argument shall be a pointer to unsigned integer.

but in your case, from the error message, it looks like payload_opt is of type u_char *.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261