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

Fix Coverity issues for pkg_editor.c #247

Merged
merged 3 commits into from
Jan 24, 2023

Conversation

IlanTruanovsky
Copy link
Contributor

This fixes all the Coverity issues in the pkg_editor.c.

Here are the Coverity issues that are fixed:

lib/pkg_editor/src/pkg_editor.c:1604:3:
  Type: Unchecked return value from library (CHECKED_RETURN)

lib/pkg_editor/src/pkg_editor.c:1584:3: Unchecked call to function
  1. path: Condition "buffer != NULL", taking true branch. lib/pkg_editor/src/pkg_editor.c:1585:5:
  2. path: Condition "input == NULL", taking true branch. lib/pkg_editor/src/pkg_editor.c:1585:5:
  3. path: Falling through to end of if statement. lib/pkg_editor/src/pkg_editor.c:1588:3:
  4. path: Falling through to end of if statement. lib/pkg_editor/src/pkg_editor.c:1594:3:
  5. path: Condition "ret != 0", taking false branch. lib/pkg_editor/src/pkg_editor.c:1604:3:
  6. check_return: Calling "mkdir(full_name, 493U)" without checking return value. This library function may fail and return an error code.

lib/pkg_editor/src/pkg_editor.c:1651:7:
  Type: Unchecked return value from library (CHECKED_RETURN)

lib/pkg_editor/src/pkg_editor.c:1584:3: Unchecked call to function
  1. path: Condition "buffer != NULL", taking true branch. lib/pkg_editor/src/pkg_editor.c:1585:5:
  2. path: Condition "input == NULL", taking true branch. lib/pkg_editor/src/pkg_editor.c:1585:5:
  3. path: Falling through to end of if statement. lib/pkg_editor/src/pkg_editor.c:1588:3:
  4. path: Falling through to end of if statement. lib/pkg_editor/src/pkg_editor.c:1594:3:
  5. path: Condition "ret != 0", taking false branch. lib/pkg_editor/src/pkg_editor.c:1610:3:
  6. path: Condition "z_info.strm.avail_in > 0", taking true branch. lib/pkg_editor/src/pkg_editor.c:1612:5:
  7. path: Condition "!read_data(&info, 20UL /* sizeof (info) */, &z_info, input)", taking false branch. lib/pkg_editor/src/pkg_editor.c:1617:5:
  8. path: Condition "info.magic != 3203399403U", taking false branch. lib/pkg_editor/src/pkg_editor.c:1625:5:
  9. path: Condition "info.kind == PACK_END", taking false branch. lib/pkg_editor/src/pkg_editor.c:1630:5:
  10. path: Condition "!read_data(name, info.name_length, &z_info, input)", taking false branch. lib/pkg_editor/src/pkg_editor.c:1638:5:
  11. path: Condition "12288UL /* 3 * 4096 */ < out_dir_length", taking true branch. lib/pkg_editor/src/pkg_editor.c:1643:5:
  12. path: Condition "full_name[12287 /* 3 * 4096 - 1 */] != 0", taking true branch. lib/pkg_editor/src/pkg_editor.c:1647:5:
  13. path: Condition "info.kind == PACK_DIR", taking true branch. lib/pkg_editor/src/pkg_editor.c:1651:7:
  14. check_return: Calling "mkdir(full_name, 493U)" without checking return value. This library function may fail and return an error code.

lib/pkg_editor/src/pkg_editor.c:500:3:
  Type: Improper use of negative value (NEGATIVE_RETURNS)

lib/pkg_editor/src/pkg_editor.c:489:3:
  1. path: Condition "f == NULL", taking false branch. lib/pkg_editor/src/pkg_editor.c:496:3:
  2. negative_return_fn: Function "ftell(f)" returns a negative number. lib/pkg_editor/src/pkg_editor.c:496:3:
  3. assign: Assigning: "file_size" = "ftell(f)". lib/pkg_editor/src/pkg_editor.c:500:3:
  4. negative_returns: "file_size" is passed to a parameter that cannot be negative. lib/pkg_editor/src/pkg_editor.c:461:3:
  4.1. path: Condition "buf == NULL", taking false branch.
lib/pkg_editor/src/pkg_editor.c:459:60:
  4.2. sizet: "size" is a size_t parameter.

lib/pkg_editor/src/pkg_editor.c:1641:5:
  Type: Out-of-bounds read (OVERRUN)

lib/pkg_editor/src/pkg_editor.c:1584:3:
  1. path: Condition "buffer != NULL", taking true branch. lib/pkg_editor/src/pkg_editor.c:1585:5:
  2. path: Condition "input == NULL", taking true branch. lib/pkg_editor/src/pkg_editor.c:1585:5:
  3. path: Falling through to end of if statement. lib/pkg_editor/src/pkg_editor.c:1588:3:
  4. path: Falling through to end of if statement. lib/pkg_editor/src/pkg_editor.c:1594:3:
  5. path: Condition "ret != 0", taking false branch. lib/pkg_editor/src/pkg_editor.c:1610:3:
  6. path: Condition "z_info.strm.avail_in > 0", taking true branch. lib/pkg_editor/src/pkg_editor.c:1612:5:
  7. path: Condition "!read_data(&info, 20UL /* sizeof (info) */, &z_info, input)", taking false branch. lib/pkg_editor/src/pkg_editor.c:1617:5:
  8. path: Condition "info.magic != 3203399403U", taking false branch. lib/pkg_editor/src/pkg_editor.c:1625:5:
  9. path: Condition "info.kind == PACK_END", taking false branch. lib/pkg_editor/src/pkg_editor.c:1630:5:
  10. path: Condition "!read_data(name, info.name_length, &z_info, input)", taking false branch. lib/pkg_editor/src/pkg_editor.c:1638:5:
  11. path: Condition "12288UL /* 3 * 4096 */ < out_dir_length", taking true branch. lib/pkg_editor/src/pkg_editor.c:1638:5:
  12. cond_at_least: Checking "12288UL < out_dir_length" implies that "out_dir_length" is at least 12289 on the true branch. lib/pkg_editor/src/pkg_editor.c:1641:5:
  13. overrun-local: Overrunning array of 12288 bytes at byte offset 12290 by dereferencing pointer "full_name + out_dir_length + 1". [Note: The source code implementation of the function has been overridden by a builtin model.]

lib/pkg_editor/src/pkg_editor.c:1411:9:
  Type: Out-of-bounds read (OVERRUN)

lib/pkg_editor/src/pkg_editor.c:1323:3:
  1. path: Condition "!append_data(&info, 20UL /* sizeof (info) */, z_info, of, 0)", taking false branch. lib/pkg_editor/src/pkg_editor.c:1330:3:
  2. path: Condition "!append_data(dir_name, name_length, z_info, of, 0)", taking false branch. lib/pkg_editor/src/pkg_editor.c:1385:5:
  3. path: Condition "8192UL /* 2 * 4096 */ < name_length", taking false branch. lib/pkg_editor/src/pkg_editor.c:1385:5:
  4. cond_at_most: Checking "8192UL < name_length" implies that "info.name_length" and "name_length" may be up to 8192 on the false branch. lib/pkg_editor/src/pkg_editor.c:1398:5:
  5. path: Condition "dir == NULL", taking false branch. lib/pkg_editor/src/pkg_editor.c:1404:5:
  6. path: Condition "entry", taking true branch. lib/pkg_editor/src/pkg_editor.c:1406:7:
  7. path: Condition "strcmp(entry->d_name, ".") != 0", taking true branch. lib/pkg_editor/src/pkg_editor.c:1406:7:
  8. path: Condition "strcmp(entry->d_name, "..") != 0", taking true branch. lib/pkg_editor/src/pkg_editor.c:1411:9:
  9. overrun-local: Overrunning array of 8192 bytes at byte offset 8192 by dereferencing pointer "full_name + name_length". [Note: The source code implementation of the function has been overridden by a builtin model.]

@IlanTruanovsky IlanTruanovsky self-assigned this Jan 16, 2023
@IlanTruanovsky IlanTruanovsky added the bug Something isn't working label Jan 16, 2023
@IlanTruanovsky IlanTruanovsky added this to the 2023.2 milestone Jan 16, 2023
Copy link
Contributor

@pcolberg pcolberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @IlanTruanovsky. Could you break these up into separate commits (but keep everything in this single PR), one commit per same kind of issue? This makes it easier to review, cherry-pick, revert, bisect, etc.

@IlanTruanovsky IlanTruanovsky force-pushed the checked-return-1 branch 3 times, most recently from 36418ad to 4672764 Compare January 17, 2023 19:31
Copy link
Contributor

@pcolberg pcolberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @IlanTruanovsky, this looks fine.

I will do a second pass tomorrow to scrutinize the string handling.

Copy link
Contributor

@pcolberg pcolberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The string bounds checks look good. I added a few suggestions for minor improvements which hopefully make the string handling code a bit easier to follow.

@IlanTruanovsky
Copy link
Contributor Author

Once I have your approval, I'll squish the rest of my commits.

Copy link
Contributor

@pcolberg pcolberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @IlanTruanovsky, this looks good, just a few minor nitpicks.

@IlanTruanovsky
Copy link
Contributor Author

IlanTruanovsky commented Jan 24, 2023

Windows build is failing with:

D:\a\fpga-runtime-for-opencl\fpga-runtime-for-opencl\lib\pkg_editor\src\pkg_editor.c(1348): error C2057: expected constant expression
D:\a\fpga-runtime-for-opencl\fpga-runtime-for-opencl\lib\pkg_editor\src\pkg_editor.c(1348): error C2466: cannot allocate an array of constant size 0
D:\a\fpga-runtime-for-opencl\fpga-runtime-for-opencl\lib\pkg_editor\src\pkg_editor.c(1348): error C2133: 'full_name': unknown size

@IlanTruanovsky
Copy link
Contributor Author

IlanTruanovsky commented Jan 24, 2023

Windows build is failing with:

D:\a\fpga-runtime-for-opencl\fpga-runtime-for-opencl\lib\pkg_editor\src\pkg_editor.c(1348): error C2057: expected constant expression
D:\a\fpga-runtime-for-opencl\fpga-runtime-for-opencl\lib\pkg_editor\src\pkg_editor.c(1348): error C2466: cannot allocate an array of constant size 0
D:\a\fpga-runtime-for-opencl\fpga-runtime-for-opencl\lib\pkg_editor\src\pkg_editor.c(1348): error C2133: 'full_name': unknown size

Strangely enough, it looks like C does not treat constant variables as actual "constant expressions"... See https://stackoverflow.com/a/5248645 and https://stackoverflow.com/a/6290979.

I'll switch it back to a macro.

@pcolberg
Copy link
Contributor

Windows build is failing with:

D:\a\fpga-runtime-for-opencl\fpga-runtime-for-opencl\lib\pkg_editor\src\pkg_editor.c(1348): error C2057: expected constant expression
D:\a\fpga-runtime-for-opencl\fpga-runtime-for-opencl\lib\pkg_editor\src\pkg_editor.c(1348): error C2466: cannot allocate an array of constant size 0
D:\a\fpga-runtime-for-opencl\fpga-runtime-for-opencl\lib\pkg_editor\src\pkg_editor.c(1348): error C2133: 'full_name': unknown size

Strangely enough, it looks like C does not treat constant variables as actual "constant expressions"... See https://stackoverflow.com/a/5248645 and https://stackoverflow.com/a/6290979.

I'll switch it back to a macro.

Thanks for giving it a try. MSVC does not support C99 properly unfortunately.

lib/pkg_editor/src/pkg_editor.c:500:3:
  Type: Improper use of negative value (NEGATIVE_RETURNS)

lib/pkg_editor/src/pkg_editor.c:489:3:
  1. path: Condition "f == NULL", taking false branch. lib/pkg_editor/src/pkg_editor.c:496:3:
  2. negative_return_fn: Function "ftell(f)" returns a negative number. lib/pkg_editor/src/pkg_editor.c:496:3:
  3. assign: Assigning: "file_size" = "ftell(f)". lib/pkg_editor/src/pkg_editor.c:500:3:
  4. negative_returns: "file_size" is passed to a parameter that cannot be negative. lib/pkg_editor/src/pkg_editor.c:461:3:
  4.1. path: Condition "buf == NULL", taking false branch.
lib/pkg_editor/src/pkg_editor.c:459:60:
  4.2. sizet: "size" is a size_t parameter.
lib/pkg_editor/src/pkg_editor.c:1641:5:
  Type: Out-of-bounds read (OVERRUN)

lib/pkg_editor/src/pkg_editor.c:1584:3:
  1. path: Condition "buffer != NULL", taking true branch. lib/pkg_editor/src/pkg_editor.c:1585:5:
  2. path: Condition "input == NULL", taking true branch. lib/pkg_editor/src/pkg_editor.c:1585:5:
  3. path: Falling through to end of if statement. lib/pkg_editor/src/pkg_editor.c:1588:3:
  4. path: Falling through to end of if statement. lib/pkg_editor/src/pkg_editor.c:1594:3:
  5. path: Condition "ret != 0", taking false branch. lib/pkg_editor/src/pkg_editor.c:1610:3:
  6. path: Condition "z_info.strm.avail_in > 0", taking true branch. lib/pkg_editor/src/pkg_editor.c:1612:5:
  7. path: Condition "!read_data(&info, 20UL /* sizeof (info) */, &z_info, input)", taking false branch. lib/pkg_editor/src/pkg_editor.c:1617:5:
  8. path: Condition "info.magic != 3203399403U", taking false branch. lib/pkg_editor/src/pkg_editor.c:1625:5:
  9. path: Condition "info.kind == PACK_END", taking false branch. lib/pkg_editor/src/pkg_editor.c:1630:5:
  10. path: Condition "!read_data(name, info.name_length, &z_info, input)", taking false branch. lib/pkg_editor/src/pkg_editor.c:1638:5:
  11. path: Condition "12288UL /* 3 * 4096 */ < out_dir_length", taking true branch. lib/pkg_editor/src/pkg_editor.c:1638:5:
  12. cond_at_least: Checking "12288UL < out_dir_length" implies that "out_dir_length" is at least 12289 on the true branch. lib/pkg_editor/src/pkg_editor.c:1641:5:
  13. overrun-local: Overrunning array of 12288 bytes at byte offset 12290 by dereferencing pointer "full_name + out_dir_length + 1". [Note: The source code implementation of the function has been overridden by a builtin model.]

lib/pkg_editor/src/pkg_editor.c:1411:9:
  Type: Out-of-bounds read (OVERRUN)

lib/pkg_editor/src/pkg_editor.c:1323:3:
  1. path: Condition "!append_data(&info, 20UL /* sizeof (info) */, z_info, of, 0)", taking false branch. lib/pkg_editor/src/pkg_editor.c:1330:3:
  2. path: Condition "!append_data(dir_name, name_length, z_info, of, 0)", taking false branch. lib/pkg_editor/src/pkg_editor.c:1385:5:
  3. path: Condition "8192UL /* 2 * 4096 */ < name_length", taking false branch. lib/pkg_editor/src/pkg_editor.c:1385:5:
  4. cond_at_most: Checking "8192UL < name_length" implies that "info.name_length" and "name_length" may be up to 8192 on the false branch. lib/pkg_editor/src/pkg_editor.c:1398:5:
  5. path: Condition "dir == NULL", taking false branch. lib/pkg_editor/src/pkg_editor.c:1404:5:
  6. path: Condition "entry", taking true branch. lib/pkg_editor/src/pkg_editor.c:1406:7:
  7. path: Condition "strcmp(entry->d_name, ".") != 0", taking true branch. lib/pkg_editor/src/pkg_editor.c:1406:7:
  8. path: Condition "strcmp(entry->d_name, "..") != 0", taking true branch. lib/pkg_editor/src/pkg_editor.c:1411:9:
  9. overrun-local: Overrunning array of 8192 bytes at byte offset 8192 by dereferencing pointer "full_name + name_length". [Note: The source code implementation of the function has been overridden by a builtin model.]
lib/pkg_editor/src/pkg_editor.c:1604:3:
  Type: Unchecked return value from library (CHECKED_RETURN)

lib/pkg_editor/src/pkg_editor.c:1584:3: Unchecked call to function
  1. path: Condition "buffer != NULL", taking true branch. lib/pkg_editor/src/pkg_editor.c:1585:5:
  2. path: Condition "input == NULL", taking true branch. lib/pkg_editor/src/pkg_editor.c:1585:5:
  3. path: Falling through to end of if statement. lib/pkg_editor/src/pkg_editor.c:1588:3:
  4. path: Falling through to end of if statement. lib/pkg_editor/src/pkg_editor.c:1594:3:
  5. path: Condition "ret != 0", taking false branch. lib/pkg_editor/src/pkg_editor.c:1604:3:
  6. check_return: Calling "mkdir(full_name, 493U)" without checking return value. This library function may fail and return an error code.

lib/pkg_editor/src/pkg_editor.c:1651:7:
  Type: Unchecked return value from library (CHECKED_RETURN)

lib/pkg_editor/src/pkg_editor.c:1584:3: Unchecked call to function
  1. path: Condition "buffer != NULL", taking true branch. lib/pkg_editor/src/pkg_editor.c:1585:5:
  2. path: Condition "input == NULL", taking true branch. lib/pkg_editor/src/pkg_editor.c:1585:5:
  3. path: Falling through to end of if statement. lib/pkg_editor/src/pkg_editor.c:1588:3:
  4. path: Falling through to end of if statement. lib/pkg_editor/src/pkg_editor.c:1594:3:
  5. path: Condition "ret != 0", taking false branch. lib/pkg_editor/src/pkg_editor.c:1610:3:
  6. path: Condition "z_info.strm.avail_in > 0", taking true branch. lib/pkg_editor/src/pkg_editor.c:1612:5:
  7. path: Condition "!read_data(&info, 20UL /* sizeof (info) */, &z_info, input)", taking false branch. lib/pkg_editor/src/pkg_editor.c:1617:5:
  8. path: Condition "info.magic != 3203399403U", taking false branch. lib/pkg_editor/src/pkg_editor.c:1625:5:
  9. path: Condition "info.kind == PACK_END", taking false branch. lib/pkg_editor/src/pkg_editor.c:1630:5:
  10. path: Condition "!read_data(name, info.name_length, &z_info, input)", taking false branch. lib/pkg_editor/src/pkg_editor.c:1638:5:
  11. path: Condition "12288UL /* 3 * 4096 */ < out_dir_length", taking true branch. lib/pkg_editor/src/pkg_editor.c:1643:5:
  12. path: Condition "full_name[12287 /* 3 * 4096 - 1 */] != 0", taking true branch. lib/pkg_editor/src/pkg_editor.c:1647:5:
  13. path: Condition "info.kind == PACK_DIR", taking true branch. lib/pkg_editor/src/pkg_editor.c:1651:7:
  14. check_return: Calling "mkdir(full_name, 493U)" without checking return value. This library function may fail and return an error code.
Copy link
Contributor

@pcolberg pcolberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @IlanTruanovsky. I made a minor modification to #undef the constants at the end of the scope in which they are used, as a workaround for the lack of C support on Windows.

@pcolberg pcolberg merged commit 5a71e64 into intel:main Jan 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants