Commit 0601c79
authored
fix(api_c): preserve exact bytes in DP_ReadFileToChar2 (deepmodeling#5763)
## Fix deepmodeling#5620
### Problem
`DP_ReadFileToChar气Char2` reported the original file size but returned a
buffer from `string_to_char`, which trims trailing whitespace before
allocating/copying. The C++ wrapper (`read_file_to_string` in
`deepmd.hpp`) then reconstructed a `std::string` with the reported
(larger) size, causing an over-read of the shorter allocation.
### Fix
Added `string_to_char_exact`, a new helper that preserves every byte
without trimming, and use it in `DP_ReadFileToChar2` and
`DP_ReadFileToChar`. Error-message paths still use the trimming
`string_to_char` since whitespace trimming is desirable there.
### Test
Added both a C++ test (`test_read_file_to_string.cc`) and a Python test
(`test_c_api_readfile.py`) that verify trailing whitespace is preserved.
The Python test uses `ctypes` to call `DP_ReadFileToChar2` directly and
the C API.
Verified: the test fails against the buggy code (over-reads garbage
bytes) and passes with the fix.
### Attribution
Generated with [opencode](https://opencode.ai) using model `glm-5.2`.
### Recommended reviewers
@njzjz (maintainer of the C API)
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
- **New Features**
- Added per-frame `charge_spin` support to DeepPot and model-deviation C
API computations.
- Introduced “version 3” compute entry points that accept `charge_spin`
(including neighbor-list variants).
- Added APIs to query the required charge-spin dimension.
- **Bug Fixes**
- File-reading C APIs now preserve exact bytes, including trailing
whitespace/newlines, and add safer size handling.
- **Tests**
- Added regression tests (C++ and Python) validating byte-for-byte file
content preservation.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: njzjz-bot <njzjz.bot@gmail.com>1 parent 716521f commit 0601c79
3 files changed
Lines changed: 120 additions & 39 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
682 | 682 | | |
683 | 683 | | |
684 | 684 | | |
685 | | - | |
686 | | - | |
| 685 | + | |
| 686 | + | |
687 | 687 | | |
688 | | - | |
689 | | - | |
| 688 | + | |
| 689 | + | |
690 | 690 | | |
691 | 691 | | |
692 | 692 | | |
693 | 693 | | |
694 | 694 | | |
695 | 695 | | |
696 | 696 | | |
697 | | - | |
698 | | - | |
699 | | - | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
700 | 701 | | |
701 | | - | |
| 702 | + | |
702 | 703 | | |
703 | | - | |
| 704 | + | |
704 | 705 | | |
705 | 706 | | |
706 | 707 | | |
| |||
727 | 728 | | |
728 | 729 | | |
729 | 730 | | |
730 | | - | |
731 | | - | |
| 731 | + | |
| 732 | + | |
732 | 733 | | |
733 | | - | |
734 | | - | |
| 734 | + | |
| 735 | + | |
735 | 736 | | |
736 | 737 | | |
737 | 738 | | |
738 | 739 | | |
739 | 740 | | |
740 | 741 | | |
741 | 742 | | |
742 | | - | |
743 | | - | |
744 | | - | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
745 | 747 | | |
746 | | - | |
| 748 | + | |
747 | 749 | | |
748 | | - | |
| 750 | + | |
749 | 751 | | |
750 | 752 | | |
751 | 753 | | |
| |||
772 | 774 | | |
773 | 775 | | |
774 | 776 | | |
775 | | - | |
776 | | - | |
| 777 | + | |
| 778 | + | |
777 | 779 | | |
778 | | - | |
779 | | - | |
| 780 | + | |
| 781 | + | |
780 | 782 | | |
781 | 783 | | |
782 | 784 | | |
| |||
787 | 789 | | |
788 | 790 | | |
789 | 791 | | |
790 | | - | |
791 | | - | |
792 | | - | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
793 | 796 | | |
794 | | - | |
| 797 | + | |
795 | 798 | | |
796 | | - | |
| 799 | + | |
797 | 800 | | |
798 | 801 | | |
799 | 802 | | |
| |||
823 | 826 | | |
824 | 827 | | |
825 | 828 | | |
826 | | - | |
827 | | - | |
| 829 | + | |
| 830 | + | |
828 | 831 | | |
829 | | - | |
830 | | - | |
| 832 | + | |
| 833 | + | |
831 | 834 | | |
832 | 835 | | |
833 | 836 | | |
| |||
838 | 841 | | |
839 | 842 | | |
840 | 843 | | |
841 | | - | |
842 | | - | |
843 | | - | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
844 | 848 | | |
845 | | - | |
| 849 | + | |
846 | 850 | | |
847 | | - | |
| 851 | + | |
848 | 852 | | |
849 | 853 | | |
850 | 854 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
1535 | 1536 | | |
1536 | 1537 | | |
1537 | 1538 | | |
| 1539 | + | |
| 1540 | + | |
| 1541 | + | |
| 1542 | + | |
| 1543 | + | |
| 1544 | + | |
| 1545 | + | |
| 1546 | + | |
| 1547 | + | |
| 1548 | + | |
| 1549 | + | |
| 1550 | + | |
| 1551 | + | |
| 1552 | + | |
| 1553 | + | |
| 1554 | + | |
| 1555 | + | |
| 1556 | + | |
| 1557 | + | |
| 1558 | + | |
| 1559 | + | |
1538 | 1560 | | |
1539 | 1561 | | |
1540 | 1562 | | |
| |||
2642 | 2664 | | |
2643 | 2665 | | |
2644 | 2666 | | |
2645 | | - | |
| 2667 | + | |
| 2668 | + | |
2646 | 2669 | | |
2647 | 2670 | | |
2648 | 2671 | | |
| |||
2656 | 2679 | | |
2657 | 2680 | | |
2658 | 2681 | | |
2659 | | - | |
2660 | | - | |
| 2682 | + | |
| 2683 | + | |
| 2684 | + | |
| 2685 | + | |
| 2686 | + | |
| 2687 | + | |
| 2688 | + | |
| 2689 | + | |
| 2690 | + | |
| 2691 | + | |
| 2692 | + | |
| 2693 | + | |
| 2694 | + | |
| 2695 | + | |
2661 | 2696 | | |
2662 | 2697 | | |
2663 | 2698 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
| 11 | + | |
10 | 12 | | |
11 | 13 | | |
12 | 14 | | |
13 | 15 | | |
| 16 | + | |
14 | 17 | | |
15 | 18 | | |
16 | 19 | | |
| |||
40 | 43 | | |
41 | 44 | | |
42 | 45 | | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
0 commit comments