Skip to content

Commit 83ca849

Browse files
committedDec 19, 2024
Merge branch 'zlib-error-handling' into 'development'
error handling for zlib uncompress See merge request damask/DAMASK!1011
2 parents 687bdb7 + c3dd28a commit 83ca849

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed
 

‎src/C_routines.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,18 @@ void signalusr2_c(void (*handler)(int)){
7171
}
7272

7373

74-
void inflate_c(const uLong *s_deflated, const uLong *s_inflated, const Byte deflated[], Byte inflated[]){
74+
void inflate_c(const uLong *s_deflated, const uLong *s_inflated, const Byte deflated[], Byte inflated[], int *stat){
7575
/* make writable copy, uncompress will write to it */
7676
uLong s_inflated_,i;
7777
s_inflated_ = *s_inflated;
7878

79-
if(uncompress((Bytef *)inflated, &s_inflated_, (Bytef *)deflated, *s_deflated) == Z_OK)
79+
*stat = 1;
80+
// https://stackoverflow.com/questions/51334741
81+
if(uncompress((Bytef *)inflated, &s_inflated_, (Bytef *)deflated, *s_deflated) == Z_OK){
82+
if (*s_inflated == s_inflated_) *stat = 0;
8083
return;
81-
else{
82-
for(i=0;i<*s_inflated;i++){
83-
inflated[i] = 0;
84-
}
8584
}
85+
else memset(inflated,0,(size_t)*s_inflated);
8686
}
8787

8888

‎src/grid/zlib.f90

+10-7
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,34 @@ module zlib
1313

1414
interface
1515

16-
subroutine inflate_C(s_deflated,s_inflated,deflated,inflated) bind(C)
17-
use, intrinsic :: ISO_C_Binding, only: C_SIGNED_CHAR, C_INT64_T
16+
subroutine inflate_C(s_deflated,s_inflated,deflated,inflated,stat) bind(C)
17+
use, intrinsic :: ISO_C_Binding, only: C_SIGNED_CHAR, C_INT64_T, C_INT
1818
implicit none(type,external)
1919

2020
integer(C_INT64_T), intent(in) :: s_deflated,s_inflated
21-
integer(C_SIGNED_CHAR), dimension(s_deflated), intent(in) :: deflated
22-
integer(C_SIGNED_CHAR), dimension(s_inflated), intent(out) :: inflated
21+
integer(C_SIGNED_CHAR), dimension(s_deflated), intent(in) :: deflated ! ok for unsigned char
22+
integer(C_SIGNED_CHAR), dimension(s_inflated), intent(out) :: inflated ! ok for unsigned char
23+
integer(C_INT), intent(out) :: stat
2324
end subroutine inflate_C
2425

2526
end interface
2627

2728
contains
2829

2930
!--------------------------------------------------------------------------------------------------
30-
!> @brief Inflate byte-wise representation
31+
!> @brief Inflate byte-wise representation.
3132
!--------------------------------------------------------------------------------------------------
3233
function zlib_inflate(deflated,size_inflated)
3334

3435
integer(C_SIGNED_CHAR), dimension(:), intent(in) :: deflated
3536
integer(pI64), intent(in) :: size_inflated
36-
3737
integer(C_SIGNED_CHAR), dimension(size_inflated) :: zlib_inflate
3838

39+
integer(C_INT) :: stat
40+
3941

40-
call inflate_C(size(deflated,kind=C_INT64_T),int(size_inflated,C_INT64_T),deflated,zlib_inflate)
42+
call inflate_C(size(deflated,kind=C_INT64_T),int(size_inflated,C_INT64_T),deflated,zlib_inflate,stat)
43+
if (stat /= 0) error stop 'inflate failed'
4144

4245
end function zlib_inflate
4346

0 commit comments

Comments
 (0)