Skip to content

[lldb] Issue with structure string #135953

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

Closed
DhruvSrivastavaX opened this issue Apr 16, 2025 · 5 comments
Closed

[lldb] Issue with structure string #135953

DhruvSrivastavaX opened this issue Apr 16, 2025 · 5 comments
Labels
lldb question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

Comments

@DhruvSrivastavaX
Copy link
Contributor

DhruvSrivastavaX commented Apr 16, 2025

As part of some of our general testing, identified the following issue:

Test case:

#include<stdio.h>
#include<string.h>

int main() {

  enum empcats {management, research, clerical, sales};
  struct
  {
    char name[30];
    float salary;
    enum empcats category;
  } employee;

  strcpy (employee.name, "Benjamin Franklin");
  employee.salary = 118.50;
  employee.category = research;

  printf ("Name = %s\n", employee.name);
  printf ("Salary = %6.2f \n", employee.salary);
  printf ("Category = %d\n", employee.category);

  if (employee.category == clerical)
    printf ("Employee category is clerical \n");
  else
  {
    printf ("Employee category is not clerical. \n");
    if (employee.category == research)
    {
      printf ("PASSED! \n");
    }
  }

  return (0);

}

Complied with clang:
clang test2.c -o test2 -g

And then this error is seen with the character array access:

(lldb) p employee
((unnamed struct))  (name = "Benjamin Franklin\0\xdfo\U00000001\0\0\0<<\U0000001c\x91\U00000001", salary = 118.5, category = research)
(lldb) p employee.name
(char[30]) "Benjamin Franklin\0\xdfo\U00000001\0\0\0<<\U0000001c\x91\U00000001"
(lldb) fr v --show-types employee
((unnamed struct)) employee = {
  (char[30]) name = "Benjamin Franklin\0\xdfo\U00000001\0\0\0<<\U0000001c\x91\U00000001"
  (float) salary = 118.5
  (empcats) category = research
}

I see this issue with Mac, Linux as well as the Demo branch in AIX.
Please check.

@llvmbot
Copy link
Member

llvmbot commented Apr 16, 2025

@llvm/issue-subscribers-lldb

Author: Dhruv Srivastava (DhruvSrivastavaX)

As part of some of our general testing, identified the following issue:

Test case:

#include&lt;stdio.h&gt;
#include&lt;string.h&gt;

int main() {

  enum empcats {management, research, clerical, sales};
  struct
  {
    char name[30];
    float salary;
    enum empcats category;
  } employee;

  strcpy (employee.name, "Benjamin Franklin");
  employee.salary = 118.50;
  employee.category = research;

  printf ("Name = %s\n", employee.name);
  printf ("Salary = %6.2f \n", employee.salary);
  printf ("Category = %d\n", employee.category);

  if (employee.category == clerical)
    printf ("Employee category is clerical \n");
  else
  {
    printf ("Employee category is not clerical. \n");
    if (employee.category == research)
    {
      printf ("PASSED! \n");
    }
  }

  return (0);

}

Complied with clang:
clang test2.c -o test2 -g

And then this error is seen with the character array access:

(lldb) p employee
((unnamed struct))  (name = "Benjamin Franklin\0\xdfo\U00000001\0\0\0&lt;&lt;\U0000001c\x91\U00000001", salary = 118.5, category = research)
(lldb) p employee.name
(char[30]) "Benjamin Franklin\0\xdfo\U00000001\0\0\0&lt;&lt;\U0000001c\x91\U00000001"
(lldb) fr v --show-types employee
((unnamed struct)) employee = {
  (char[30]) name = "Benjamin Franklin\0\xdfo\U00000001\0\0\0&lt;&lt;\U0000001c\x91\U00000001"
  (float) salary = 118.5
  (empcats) category = research
}

I see this issue with Mac, Linux as well as our Demo branch in AIX.
Please check.

@bulbazord
Copy link
Member

I think LLDB's current behavior is expected and fine. Allow me to elaborate.

From LLDB's perspective, a char[30] is not a const char *. C-style strings are expected to end in a null terminator, and you seem to be implying lldb should stop showing you characters in the buffer when it sees the null terminator after "Benjamin Franklin". You can create a const char * from a char[30], but they are not the same thing. You can assign your own semantics to a char[30], for example, maybe it could be a const char **? Something like: "foo\0bar\0baz\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0". LLDB would want to show you the entire buffer here instead of stopping at the first c-style string it finds in the buffer.

@jimingham
Copy link
Collaborator

jimingham commented Apr 16, 2025

I agree with Alex. lldb has no way of knowing how you meant to use that char[30], so it should not assume you meant a null-terminated C-string since assuming that would lose information. There are many other possible uses of this data structure in which eliding everything after the first \0 would be wrong.

@labath
Copy link
Collaborator

labath commented Apr 22, 2025

It's also consistent with gdb:

(gdb) p X
$1 = "hello", '\000' <repeats 24 times>

(well, mostly consistent, I think lldb doesn't have the "repeats N times" thing -- but it would be nice if it did)

@labath labath closed this as completed Apr 22, 2025
@EugeneZelenko EugeneZelenko added the question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead! label Apr 22, 2025
@DhruvSrivastavaX
Copy link
Contributor Author

Okay, thanks for the clarification everyone

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lldb question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!
Projects
None yet
Development

No branches or pull requests

6 participants