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

clang/asan: strtol() interceptor doesn't report out-of-range values (incorrect behavior of sanitized program) #121832

Open
kalibera opened this issue Jan 6, 2025 · 0 comments
Labels
compiler-rt:asan Address sanitizer

Comments

@kalibera
Copy link

kalibera commented Jan 6, 2025

It seems that strtol() under clang/asan doesn't set errno to ERANGE when the resulting value is out of range. I've ran into this on Windows with Msys2 build of llvm (clang64 toolchain).

The example further below, compiled via "clang -o t test.c" correctly prints:

Input: 12345689012
LONG_MAX=2147483647
Out of range

However, when compiled via " clang -o t test.c -fsanitize=address", it (incorrectly) prints:

Input: 12345689012
LONG_MAX=2147483647
Result: 2147483647

This is with clang version 19.1.6 on x86_64 and errno remains at value 0.

The example is:

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int main(int argc, char **argv) {
  char *s = "12345689012";
  char *endp;
  long res;

  printf("Input: %s\n", s);
  printf("LONG_MAX=%ld\n", LONG_MAX);
  errno = 0;
  res = strtol(s, &endp, 10);
  if (*endp != '\0') {
    printf("Incomplete.\n");
    return 1;
  }
  if (errno == ERANGE) {
    printf("Out of range");
    return 2;
  }
  printf("Result: %ld\n", res);
  return 0;
}

The practice of setting errno to zero before calling strtol() and checking errno after the call follows POSIX recommendation.

@llvmbot llvmbot added the clang Clang issues not falling into any other category label Jan 6, 2025
@EugeneZelenko EugeneZelenko added compiler-rt:asan Address sanitizer and removed clang Clang issues not falling into any other category labels Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler-rt:asan Address sanitizer
Projects
None yet
Development

No branches or pull requests

3 participants