Avoid potential negative array index access to cached text.
[LibreOffice.git] / desktop / win32 / source / unoinfo.cxx
blob14cee8819ddec6a23d0e8b8e6fd06cdc2f494c3e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <cstddef>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <wchar.h>
25 #define WIN32_LEAN_AND_MEAN
26 #include <windows.h>
28 #include <tools/pathutils.hxx>
30 #define MY_LENGTH(s) (sizeof (s) / sizeof *(s) - 1)
31 #define MY_STRING(s) (s), MY_LENGTH(s)
33 namespace {
35 wchar_t * getBrandPath(wchar_t * path) {
36 DWORD n = GetModuleFileNameW(nullptr, path, MAX_PATH);
37 if (n == 0 || n >= MAX_PATH) {
38 exit(EXIT_FAILURE);
40 return tools::filename(path);
43 void writeNull() {
44 if (fwrite("\0\0", 1, 2, stdout) != 2) {
45 exit(EXIT_FAILURE);
49 void writePath(
50 wchar_t const * frontBegin, wchar_t const * frontEnd,
51 wchar_t const * backBegin, std::size_t backLength)
53 wchar_t path[MAX_PATH];
54 wchar_t * end = tools::buildPath(
55 path, frontBegin, frontEnd, backBegin, backLength);
56 if (end == nullptr) {
57 exit(EXIT_FAILURE);
59 std::size_t n = (end - path) * sizeof (wchar_t);
60 if (fwrite(path, 1, n, stdout) != n) {
61 exit(EXIT_FAILURE);
67 int wmain(int argc, wchar_t ** argv, wchar_t **) {
68 if (argc == 2 && wcscmp(argv[1], L"c++") == 0) {
69 wchar_t path[MAX_PATH];
70 wchar_t * pathEnd = getBrandPath(path);
71 writePath(path, pathEnd, MY_STRING(L""));
72 } else if (argc == 2 && wcscmp(argv[1], L"java") == 0) {
73 if (fwrite("1", 1, 1, stdout) != 1) {
74 exit(EXIT_FAILURE);
76 wchar_t path[MAX_PATH];
77 wchar_t * pathEnd = getBrandPath(path);
78 writePath(path, pathEnd, MY_STRING(L"classes\\libreoffice.jar"));
79 writeNull();
80 writePath(path, pathEnd, MY_STRING(L""));
81 } else {
82 exit(EXIT_FAILURE);
84 exit(EXIT_SUCCESS);
87 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */