#can sprintf be used to initialize values

17 messages · Page 1 of 1 (latest)

ebon shore
#
void initAnimal(int i, SpeciesType s, char *n, char *g, int a, AnimalType *an)
{
  an->species = s;
  an->age = a;
  sprintf(an->gender, "%31s", g);
  sprintf(an->name, "%31s", n);
  an->id = i;
}
```like is the above correct
vernal burrowBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

coarse torrent
#

assuming those fields are arrays, that would work

#

though this function seems pretty unnecessary

#

looks like it will add a bunch of spaces to the end of the string if it is too short too

#

very strange

ebon shore
coarse torrent
delicate oak
#

use snprintf and %s

#

actually, just use memcpy since you're not formatting anything

#
void initAnimal(int i, SpeciesType s, const char *n, const char *g, int a, AnimalType *an)
{
  an->species = s;
  an->age = a;
  const n_len = strlen(n);
  const g_len = strlen(g);
  /* too long */
  if(g_len < 32)
    memcpy(an->gender, g, g_len + 1);
  else
    memcpy(an->gender, "TOO LONG", 9);

  if(n_len < 32)
    memcpy(an->name, n, n_len + 1);
  else
    memcpy(an->name, "TOO LONG", 9);
  an->id = i;
}
#

@ebon shore your current code breaks if you supply a larger string btw

#

;compile -fsanitize=address

#include <string.h>
#include <stdio.h>
typedef int SpeciesType;
typedef struct {
  SpeciesType species;
  int         age;
  int         id;
  char        gender[32];
  char        name[32];
} AnimalType;

void initAnimal(int i, SpeciesType s, char *n, char *g, int a, AnimalType *an)
{
  an->species = s;
  an->age = a;
  sprintf(an->gender, "%31s", g);
  sprintf(an->name, "%31s", n);
  an->id = i;
}

int main(void) {
    AnimalType a;
    initAnimal(1337, 0xbeef, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 20, &a);
}
gloomy widgetBOT
#
Compiler Output
=================================================================
==1==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7fff1a89938c at pc 0x7f0464235bbd bp 0x7fff1a8991d0 sp 0x7fff1a898980
WRITE of size 111 at 0x7fff1a89938c thread T0
    #0 0x7f0464235bbc in vsprintf (/opt/compiler-explorer/gcc-12.2.0/lib64/libasan.so.8+0x76bbc)
    #1 0x7f0464235dbe in __interceptor_sprintf (/opt/compiler-explorer/gcc-12.2.0/lib64/libasan.so.8+0x76dbe)
    #2 0x401249 in initAnimal /app/example.c:16
    #3 0x401362 in main /app/example.c:23
    #4 0x7f0463ff1082 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24082)
    #5 0x4010cd in _start (/app/output.s+0x4010cd)

Address 0x7fff1a89938c is located in stack of thread T0 at offset 124 in frame
    #0 0x4012c2 in main /app/example.c:21

  This frame has 1 object(s):
    [48, 124) 'a' (line 22) <== Memory access at offset 124 overflows this variable
HINT: this may be a false positive if your program uses some custom stack unwind
vernal burrowBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.

ebon shore
#

a bit late but thank you both for the reply!!

#

!solved