TODOPIC

Lenguajes de programación para PC => C, C#, C++ => Mensaje iniciado por: DominusDRR en 24 de Junio de 2022, 12:35:52

Título: error: '%s' directive writing up to 255 bytes into a region of size between 153
Publicado por: DominusDRR en 24 de Junio de 2022, 12:35:52
Hola.

Tengo el siguiente código que compila sin problemas:

Código: C
  1. char bufferx[] =
  2.                  "{"                             \
  3.                     "\"Tipo\":1"                    \
  4.                     ","                             \
  5.                     "\"Uid\":\"abcdefghi\""  \
  6.                     ","                             \
  7.                     "\"Puerto\":6777"               \
  8.                     ","                             \
  9.                     "\"TipoDeConeccion\":0"         \
  10.                                                     \
  11.                     "}" ;
  12.                                  
  13.                 sprintf(buffer, "POST /%s HTTP/1.1\r\n"
  14.                     "Host: %s\r\n"
  15.                     "Connection: close\r\n"
  16.                     "Content-type: application/json\r\n"
  17.                     "Content-length: %d\r\n\r\n"
  18.                     "%s",
  19.                 appethData.path ? appethData.path : "null" ,appethData.host,strlen(bufferx),bufferx);

Luego lo he modificado de la siguiente manera:

Código: C
  1. char bufferx[256];
  2.    retornnarJSONCorrespondiente(SOLICITANDO_CONEXION, bufferx);
  3.    sprintf(buffer, "POST /%s HTTP/1.1\r\n"
  4.                     "Host: %s\r\n"
  5.                     "Connection: close\r\n"
  6.                     "Content-type: application/json\r\n"
  7.                     "Content-length: %d\r\n\r\n"
  8.                     "%s",
  9.                 appethData.path ? appethData.path : "null" ,appethData.host,strlen(bufferx),bufferx);

Donde retornnarJSONCorrespondiente es de la siguiente manera:

Código: C
  1. void retornnarJSONCorrespondiente(unsigned char procesoCliente, char *retorno)
  2. {
  3.     unsigned char i;
  4.     char bufferJSON[256];
  5.     memset(bufferJSON,0x00, sizeof(bufferJSON));
  6.     switch(procesoCliente)
  7.     {
  8.         case SOLICITANDO_CONEXION:
  9.         {
  10.             strncpy(bufferJSON,
  11.                     "{"                             \
  12.                     "\"Tipo\":1"                    \
  13.                     ","                             \
  14.                     "\"Uid\":\"abcdefghi\""  \
  15.                     ","                             \
  16.                     "\"Puerto\":6777"               \
  17.                     ","                             \
  18.                     "\"TipoDeConeccion\":0"         \
  19.                                                     \
  20.                     "}"  
  21.                     ,255); //longitud - 0x01
  22.             break;
  23.         }
  24.         default: break;
  25.     }
  26.     i = 0x00;
  27.     while(bufferJSON[i] > 0x00)
  28.     {
  29.         *retorno = bufferJSON[i];
  30.         i++;
  31.         retorno++;
  32.     }
  33.     *retorno = 0x00; //pongo en null el último byte
  34. }

Pero al compilar el proyecto, obtengo el siguiente error:

../src/appeth.c:215:33: error: '%s' directive writing up to 255 bytes into a region of size between 153 and 159 [-Werror=format-overflow=]
                 sprintf(buffer, "POST /%s HTTP/1.1\r\n"


La única diferencia entre el primer y el segundo caso, es que  bufferx inicialmente no tiene un tamaño definido, mientras que en el otro tiene un tamaño de 256 bytes, el string que se agrega, no sobre pasa 256 bytes y también he aumentado a 512 el tamaño del mismo y el problema es el mismo.

¿Alguna sugerencia o comentario?

Saludos.
Título: Re:error: '%s' directive writing up to 255 bytes into a region of size between 153
Publicado por: DominusDRR en 24 de Junio de 2022, 19:36:39
Me respondo a mi mismo.

El arreglo que recoge toda la información, buffer, estaba definido con un tamaño de 256 bytes:

Código: C
  1. char buffer[256]

El problema sucede a pesar que no se sobrepasa esa cantidad de bytes (256).

Así que lo redefiní a 512 bytes así:

Código: C
  1. char buffer[512]

Y el problema de compilación ha desaparecido.