Skip to content

Commit 6ac0381

Browse files
committed
fix(stablediffusion-ggml): NULL terminate options array to prevent segfault
Signed-off-by: Richard Palethorpe <[email protected]>
1 parent edd33c8 commit 6ac0381

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

backend/go/stablediffusion-ggml/gosd.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ int load_model(char *model, char* options[], int threads, int diff) {
104104
char *scheduler = "";
105105
char *sampler = "";
106106

107+
fprintf(stderr, "parsing options\n");
108+
107109
// If options is not NULL, parse options
108110
for (int i = 0; options[i] != NULL; i++) {
109111
char *optname = strtok(options[i], ":");
@@ -132,10 +134,13 @@ int load_model(char *model, char* options[], int threads, int diff) {
132134
}
133135
}
134136

137+
fprintf(stderr, "parsed options\n");
138+
135139
int sample_method_found = -1;
136140
for (int m = 0; m < SAMPLE_METHOD_COUNT; m++) {
137141
if (!strcmp(sampler, sample_method_str[m])) {
138142
sample_method_found = m;
143+
fprintf(stderr, "Found sampler: %s\n", sampler);
139144
}
140145
}
141146
if (sample_method_found == -1) {

backend/go/stablediffusion-ggml/gosd.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ func (sd *SDGGML) Load(opts *pb.ModelOptions) error {
3737

3838
size := C.size_t(unsafe.Sizeof((*C.char)(nil)))
3939
length := C.size_t(len(opts.Options))
40-
options = (**C.char)(C.malloc(length * size))
41-
view := (*[1 << 30]*C.char)(unsafe.Pointer(options))[0:len(opts.Options):len(opts.Options)]
40+
options = (**C.char)(C.malloc((length + 1) * size))
41+
view := (*[1 << 30]*C.char)(unsafe.Pointer(options))[0:len(opts.Options) + 1:len(opts.Options) + 1]
4242

4343
var diffusionModel int
4444

@@ -66,6 +66,7 @@ func (sd *SDGGML) Load(opts *pb.ModelOptions) error {
6666
for i, x := range oo {
6767
view[i] = C.CString(x)
6868
}
69+
view[len(oo)] = nil
6970

7071
sd.cfgScale = opts.CFGScale
7172

0 commit comments

Comments
 (0)