RFR: 8262491: AArch64: CPU description should contain compatible board list [v6]
Yasumasa Suenaga
ysuenaga at openjdk.java.net
Fri Mar 12 05:19:06 UTC 2021
On Thu, 11 Mar 2021 12:22:10 GMT, Yasumasa Suenaga <ysuenaga at openjdk.org> wrote:
>> No, if `open()` fails we should return straight away, with an empty string. That needs an addition.
>> We must, however, terminate the string with 0 at the correct point, at the end of the bytes read. Otherwise ` strlen() `reads uninitialized memory. If the `read()` fails, we must return an empty string.
>
> I think my latest commit includes your suggestion:
>
> * returns empty string ( `\0` ) when `open()` failed.
> * returns empty string when `read()` failed or read nothing (returns `0` )
> * add `\0` to `buf[read_sz]` just after `read()` call, and skip it at the loop - it can be assumed `\0` is set to tail of `buf`
>
> Or should I change as following for readability?
>
> int fd = open("/proc/device-tree/compatible", O_RDONLY);
> if (fd == -1) {
> *buf = '\0';
> return;
> }
>
> ssize_t read_sz = read(fd, buf, buflen - 1);
> close(fd);
> if (read_sz <= 0) {
> *buf = '\0';
> return;
> }
>
> // Add '\0' to the tail
> buf[read_sz] = '\0';
> // Replace '\0' to ' '
> for (char *ch = buf; ch < buf + read_sz; ch++) {
> if (*ch == '\0') {
> *ch = ' ';
> }
> }
I tested current PR manually with this Gist code, it works fine without buffer overrun.
https://gist.github.com/YaSuenag/bdc73c3540335a096fa289ead36ca8b5
-------------
PR: https://git.openjdk.java.net/jdk/pull/2759
More information about the hotspot-dev
mailing list