Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

copy_string error #37

Closed
hailihu opened this issue Sep 30, 2020 · 2 comments
Closed

copy_string error #37

hailihu opened this issue Sep 30, 2020 · 2 comments

Comments

@hailihu
Copy link

hailihu commented Sep 30, 2020

For a Landsat scene (LC81210622020202LGN00), force-l2ps exits with error "cannot copy, string too long.". This happens at line 699 of src/lower-level/gas-ll.c:
copy_string(source, NPOW_02, tokenptr)
because apparently tokenptr contains a too long string, printing it gives "MYD". This happens when reading a daily WVP_***.txt file where "MYD" is in the last column. Perhaps there is no end-of-string character for tokenptr? Anyways, replacing line 699 with:
strncpy(source, tokenptr, NPOW_02)
makes the preprocessing succeed.

@davidfrantz
Copy link
Owner

davidfrantz commented Oct 10, 2020

Hi Haili,

thanks for your suggestion. I really appreciate this.

But this is already fixed now with FORCE 3.5.2.

I would however suggest you not using your proposed fix as this is insecure (have a read here), and actually fails in this case.

The problem here is, that I forgot to replace the EOL character in the reading funtion, i.e. tokenptr = "MOD\n", should actually be tokenptr = "MOD\0"

This means that the copied string won't be null-terminated, which might cause other problems.

The copy_string function already did as you suggest, but adds a test whether the string is null-terminated after copying. As we were copying 4 (tokenptr = "MOD\n) instead of 3 characters (tokenptr = "MOD\0"), there was no space for the terminating NULL, and the function fails safely:

void copy_string(char *dst, size_t size, const char *src){

  strncpy(dst, src, size);
  if (dst[size-1] != '\0'){
    printf("cannot copy, string too long:\n%s\n", src);
    exit(1);
  }

  return;
}

The fix is to include this line here in src/lower-level/gas-ll.c in l. 682:

buffer[strcspn(buffer, "\r\n#")] = 0;

This even makes it safe to use a file with Windows EOL.

Cheers,

David

@hailihu
Copy link
Author

hailihu commented Oct 12, 2020

Thanks David,
I tested 3.5.2 and it works now. It wasn't really a fix I proposed, just I workaround so I could identify the source of error and continue processing. Good you have a solid fix now, thanks!
Cheers, Haili

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants